understanding email validation using JavaScript

AlwaysALearner picture AlwaysALearner · Feb 22, 2013 · Viewed 20.1k times · Source

I am new to JavaScript and found this JavaScript code in the internet that validates the given email (no issue with the code) -

<html>
<h2>Email Validation</h2>
<script language = "Javascript">
function checkEmail(emailId) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailId)){
document.write("You have entered valid email.");
return true;
}    
return false;
}

function ValidateEmail(){
    var emailID=document.form.email;

    if ((emailID.value==null)||(emailID.value=="")){
        alert("Please Enter your Email ID")
        emailID.focus()
        return false
    }

    if (checkEmail(emailID.value)==false){
        emailID.value=""
        alert("Invalid Email Adderess");
        emailID.focus()
        return false
    }
        alert('valid');
        return true
 }
</script>

<form name="form" method="post" onSubmit="return ValidateEmail()">    
Enter an Email Address : <input type="text" name="email" size="30"><br>    
<input type="submit" name="Submit" value="Submit">    
</form>

</html>

I have no issues with the code, but I somehow failed to understand what the regular expression /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ means. I don't understand what each and every part of regular expression means. Please enlighten me.

Answer

Siddiqui picture Siddiqui · Feb 22, 2013
  1. The two forward-slashes /.../ contains a regexe.

  2. The leading ^ and trailing $ match the beginning and the ending of the input string, respectively. That is, the entire input string shall match with this regexe, instead of a part of the input string.

  3. \w+ matches 1 or more word characters (a-z, A-Z, 0-9 and underscore).

  4. [.-] matches character . or -. We need to use . to represent . as . has special meaning in regexe. The \ is known as the escape code, which restore the original literal meaning of the following character.

  5. [.-]? matches 0 or 1 occurrence of [.-].

  6. Again, \w+ matches 1 or more word characters.

  7. ([.-]?\w+)* matches 0 or more occurrences of [.-]?\w+.

  8. The sub-expression \w+([.-]?\w+)* is used to match the username in the email, before the @ sign. It begins with at least one word character (a-z, A-Z, 0-9 and underscore), followed by more word characters or . or -. However, a . or - must follow by a word character (a-z, A-Z, 0-9 and underscore). That is, the string cannot contain "..", "--", ".-" or "-.". Example of valid string are "a.1-2-3".

  9. The @ matches itself.

  10. Again, the sub-expression \w+([.-]?\w+)* is used to match the email domain name, with the same pattern as the username described above.

  11. The sub-expression .\w{2,3} matches a . followed by two or three word characters, e.g., ".com", ".edu", ".us", ".uk", ".co".

  12. (.\w{2,3})+ specifies that the above sub-expression shall occur one or more times, e.g., ".com", ".co.uk", ".edu.sg" etc.

Reference