Simple Email validation regular Expression for Javascript

Following regular expression will validate emails.  

var re =/^[A-Z0-9a-z\._+]+@[A-Za-z0-9\.-]+\.([A-Za-z]{2,4})$/;

re.test(value);

We will discuss above regular expression part by part.
Examples - 
part1@part2.part3
1. abcd+ef@bbb.com
2. cccc_efg@bbb.lk
3. cccc.efg@bbb.net
^ ==> Start of the expression
Part1
[A-Z0-9a-z\._+]+  ==> There can be many of  A-Z upper case characters, a-z lower case characters, 0-9 numbers, dots and plus signs.
@ ==> There must be @ sign after first part of the email
Part2
[A-Za-z0-9\.-]+ ==> After @ sign there can be same kind of character sets as part 1 (part 2 of email)
\. ==> There must be dot character before 3rd part of the email. It should be properly escaped. That is why we use backslash (\)
Part3
([A-Za-z]{2,4}) ==> There are must be two to four characters({2,4})including  A-Z upper case characters or a-z lower case characters.  Curly brackets specify the range of characters. So, 2 is minimum number of characters and 4 is maximum number of characters
Regular Expression (Regex) javascript test() Method
This test() method of javacription is used test for match in a string such as emails. If there is a match found it returns true otherwise false.
re.test(value);

value variable in test() method is the value that comes to function to validate such as email or any other string


Enjoy

Comments

Popular posts from this blog

Laravel Sail on Windows issue - "cmdlet Invoke-WebRequest at command pipeline position 1"

GIT Error: The following untracked working tree files would be overwritten by merge:

How to point Magento multi websites to domains