Skip to content

validate input string with regex #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions L-I/0011 RegexInputValidation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Regex Input Validation

## Problem

Write a function in JavaScript to validate inputs using regex, taking two parameters: input string and type (email, name, address, zip code, tel).

## Solution

```javascript
function validateInput(input, type) {
// Create a regular expression for the given type
const regex = {
email:
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
name: /^[a-zA-Z]+(?:\s[a-zA-Z]+)*$/,
address: /^[a-zA-Z0-9\s,.'-]+$/,
"zip code": /^\d{5}(-\d{4})?$/,
tel: /^\d{10}$/,
};

// Test the input against the regular expression
return regex[type].test(input);
}
```

## How it works

- The function takes two parameters: input string and type (email, name, address, zip code, tel).
- It creates a regular expression for the given type using the regex object.
- It tests the input against the regular expression using the test() method.
- It returns true if the input is valid, and false otherwise.

## References

Regular expressions in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

## Problem Added By

- [GitHub](https://github.com/aladin002dz)
- [LinkedIn](https://www.linkedin.com/in/mahfoudh-arous/)

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.
1 change: 1 addition & 0 deletions L-I/0011 RegexInputValidation/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
15 changes: 15 additions & 0 deletions L-I/0011 RegexInputValidation/regexInputValidation_algorithm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function validateInput(input, type) {
// Create a regular expression for the given type
const regex = {
"email": /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
"name": /^[A-Z][A-Za-zéç]+(\s[A-Z][A-Za-zéç]+)*$/,
"address": /^[A-Za-z0-9éç°',]+(\s[A-Za-z0-9éç°',]+)*$/,
"zip code": /^\d{5}(-\d{4})?$/,
"tel": /^\d{3}-\d{3}-\d{4}$/
};

// Test the input against the regular expression
return regex[type].test(input);
}

module.exports = validateInput;
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const validateInput = require('./regexInputValidation_algorithm')

describe('validateInput()', () => {
test('should validate a valid email address', () => {
const email = 'test@example.com';
const isValidEmail = validateInput(email, 'email');

expect(isValidEmail).toBe(true);
});

test('should not validate an invalid email address', () => {
const email = 'test@example';
const isValidEmail = validateInput(email, 'email');

expect(isValidEmail).toBe(false);
});

test('should validate a valid name', () => {
const name = 'John Doe';
const isValidName = validateInput(name, 'name');

expect(isValidName).toBe(true);
});

test('should not validate an invalid name', () => {
const name = '12345';
const isValidName = validateInput(name, 'name');

expect(isValidName).toBe(false);
});

test('should validate a valid address', () => {
const address = '123 Main Street, Anytown, CA 91234';
const isValidAddress = validateInput(address, 'address');

expect(isValidAddress).toBe(true);
});

test('should not validate an invalid address', () => {
const address = ' ';
const isValidAddress = validateInput(address, 'address');

expect(isValidAddress).toBe(false);
});

test('should validate a valid zip code', () => {
const zipCode = '91234';
const isValidZipCode = validateInput(zipCode, 'zip code');

expect(isValidZipCode).toBe(true);
});

test('should not validate an invalid zip code', () => {
const zipCode = '123456';
const isValidZipCode = validateInput(zipCode, 'zip code');

expect(isValidZipCode).toBe(false);
});

test('should validate a valid telephone number', () => {
const telephoneNumber = '123-456-7890';
const isValidTelephoneNumber = validateInput(telephoneNumber, 'tel');

expect(isValidTelephoneNumber).toBe(true);
});

test('should not validate an invalid telephone number', () => {
const telephoneNumber = '123-456';
const isValidTelephoneNumber = validateInput(telephoneNumber, 'tel');

expect(isValidTelephoneNumber).toBe(false);
});
});