• Post category:JavaScript
  • Reading time:3 mins read

Today we will provide you a list of regular expression examples to validate the value in JavaScript.

While you are working with the real project then you might need to implement certain validation so these examples are helpful to you over there.

Syntax

var value = "xxxxxx";
var regex = /^xxxxxxxxx$/;
return regex.test(value);

Let’s take an example for different types of validations using regular expressions. It’s also possible to use different regex for the same validation.

Regular expression with examples

  1. Email validation
  2. Number validation
  3. Number validation for 10 digit
  4. Allow symbols in number validation
  5. URL validation
  6. Card CVV validation
  7. Card expiry date validation
  8. Percentage validation
  9. Password validation

1. Email validation

Regular expression for email validation.

var regEx = /^(([^<>()[\]\\.,;:\[email protected]"]+(\.[^<>()[\]\\.,;:\[email protected]"]+)*)|(".+"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

regEx.test("abc"); // Output: false
regEx.test("[email protected]"); // Output: false
regEx.test("[email protected]"); // Output: false
regEx.test("[email protected]"); // Output: false
regEx.test("[email protected]"); // Output: true
regEx.test("[email protected]"); // Output: true

2. Number validation

Use below regular expression to validate the number value.

var regEx = /^[0-9]*$/;

regEx.test("abc"); // Output: false
regEx.test(); // Output: false
regEx.test("0123"); // Output: true
regEx.test("0123456789"); // Output: true

3. Number validation for 10 digit

Regular expression to validate the 10 digit number.

var regEx = /^[0-9]{10}$/;

regEx.test("abc"); // Output: false
regEx.test(); // Output: false
regEx.test("0123456789"); // Output: true

4. Allow symbols in number validation

Regular expression to allow the few symbols like -,+,(,) including space in number validation between 10 to 14 digits.

var regEx = /^(?=.*[0-9])[- +()0-9]{10,14}$/;

regEx.test("56789"); // Output: false
regEx.test("(999) 999-99.99"); // Output: false
regEx.test("(999) 999-9999"); // Output: true

5. URL validation

In order to validate the URL, use the regular expression below.

var regEx = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/;

regEx.test("cluemediator"); // Output: false
regEx.test("cluemediator.c"); // Output: false
regEx.test("cluemediator.com"); // Output: true
regEx.test("www.cluemediator.com"); // Output: true
regEx.test("test://www.cluemediator.com"); // Output: false
regEx.test("http://www.cluemediator.com"); // Output: true
regEx.test("https://www.cluemediator.com"); // Output: true

6. Card CVV validation

Use below regular expression to validate the card CVV number.

var regEx = /^[0-9]{3,4}$/;

regEx.test(""); // Output: false
regEx.test("123"); // Output: true
regEx.test("1234"); // Output: true
regEx.test("123456"); // Output: false

7. Card expiry date validation

To validate the card expiry Date, use the regular expression below. Here we will consider the value in mm/YY format.

var regEx = /(0[1-9]|10|11|12)\/[0-9]{2}|\./;

regEx.test(""); // Output: false
regEx.test("8/22"); // Output: false
regEx.test("08/22"); // Output: true
regEx.test("12/22"); // Output: true
regEx.test("13/22"); // Output: false
regEx.test("08-22"); // Output: false

8. Percentage validation

Use below regular expression to validate the percentage value.

var regEx = /^((\d{0,2}(\.\d{1,4})?)|100)$/;

regEx.test("10"); // Output: true
regEx.test("67.22"); // Output: true
regEx.test("999"); // Output: false
regEx.test("100"); // Output: true

Here we allowed the 4 digits after the decimal point.

9. Password validation

Check below regular expression to validate the password in certain criteria. Like password must contain at least 8 characters, including uppercase, lowercase and numbers.

var regEx = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;

regEx.test("123456789"); // Output: false
regEx.test("123456789d"); // Output: false
regEx.test("123456789dD"); // Output: true
regEx.test("[email protected]"); // Output: false
regEx.test("[email protected]"); // Output: true

I hope you find this article helpful.
Thank you for reading. Happy Coding..!!

Leave a Reply