From e2a9edb4ca01c8c0708e077d04ea473f49d9a87d Mon Sep 17 00:00:00 2001 From: aladin002dz Date: Thu, 12 Oct 2023 15:28:21 +0100 Subject: [PATCH] validate input string with regex --- L-I/0011 RegexInputValidation/README.md | 46 ++++++++++++ L-I/0011 RegexInputValidation/jest.config.js | 1 + .../regexInputValidation_algorithm.js | 15 ++++ .../regexInputValidation_algorithm.test.js | 73 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 L-I/0011 RegexInputValidation/README.md create mode 100644 L-I/0011 RegexInputValidation/jest.config.js create mode 100644 L-I/0011 RegexInputValidation/regexInputValidation_algorithm.js create mode 100644 L-I/0011 RegexInputValidation/regexInputValidation_algorithm.test.js diff --git a/L-I/0011 RegexInputValidation/README.md b/L-I/0011 RegexInputValidation/README.md new file mode 100644 index 0000000..289292a --- /dev/null +++ b/L-I/0011 RegexInputValidation/README.md @@ -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. diff --git a/L-I/0011 RegexInputValidation/jest.config.js b/L-I/0011 RegexInputValidation/jest.config.js new file mode 100644 index 0000000..6f31cf5 --- /dev/null +++ b/L-I/0011 RegexInputValidation/jest.config.js @@ -0,0 +1 @@ +{ } \ No newline at end of file diff --git a/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.js b/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.js new file mode 100644 index 0000000..c7a1ab5 --- /dev/null +++ b/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.js @@ -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; \ No newline at end of file diff --git a/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.test.js b/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.test.js new file mode 100644 index 0000000..be46d3a --- /dev/null +++ b/L-I/0011 RegexInputValidation/regexInputValidation_algorithm.test.js @@ -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); + }); +});