Skip to content

Email regex JavaScript Validation | Example code

  • by

An email is a string (a subset of ASCII characters) separated into two parts by @ symbol. A “Unique_personal_id” and a domain. It can easily validate by using regex JavaScript.

reg ex – Regular + expression

First part can contains the following ASCII characters.

  • Uppercase (A-Z) and lowercase (a-z) English letters.
  • Digits (0-9).
  • Characters ! # $ % & ‘ * + – / = ? ^ _ ` { | } ~
  • Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.

Second part

domain name [for example com, org, net, in, us, info] part contains letters, digits, hyphens, and dots.

Example code of Email regex JavaScript

Simple complete HTML example of JavaScript code to validate an email id :-

In this example using input fields for email address and button for click. if the email id valid then it will show a Valid alert box otherwise will show invalid.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        function ValidateEmail(inputText)
        {
            var mailformat = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
            if(inputText.value.match(mailformat))
            {
                alert("Valid email address!");
                return true;
            }
            else
            {
                alert("Invalid email address!");
                return false;
            }
        }
    </script>
</head>
<body>

    
    <form name="form1" action="#">
        <input type='text' name='text1'/>

        <input type="submit" name="submit" value="Submit" onclick="ValidateEmail(document.form1.text1)"/>
    </form>

    <script src="email-validation.js"></script>
</body>
</html>

Output:

HTML Example code of Email regex JavaScript

What is a Regular Expression Pattern for email id?

Answer: Here is email validation Regular Expression Pattern code:

/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

Do comment if you have any problems, doubts, and suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *