All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Regular Expression For Email Validation In JavaScript

Last Updated : Mar 11, 2024

Regular Expression For Email Validation In JavaScript

in this tutorial we will show you the solution of regular expression for email validation in JavaScript, here we defined label with email input field and submit button it had onclick event for load emValid() function when user submit their email and clicks submit button.

Then it checks with defined regular expression of email pattern for identify whether user entered email is valid or not respective message will display on webpage.

Step By Step Guide On Regular Expression For Email Validation In JavaScript :-

Here we defined one label ‘Enter Email ID’ and one input field for collect user email then submit button with onclick event.

When user clicks on submit it loads ‘emValid()’ function call within function we collects user email id and stored to variable ‘email’ by input field id ‘em’ and pattern contain correct email pattern regular expression stored to variable ‘ptrn’.

Using if() condition with match() function we checks user entered mail id match with pattern if matched then it will display message ‘Valid Input’ using alert() method, otherwise it will display ‘Invalid Input’ message on webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>Email Validation</title>
    </head>
    <body>
        <label for="em">Enter Email ID</label>
        <input type="text" id="em">
        <input type="submit" value="Submit" onclick="emValid()">
        <script>
            function emValid(){
                var email=document.getElementById('em');
                var ptrn=/\S+@\S+\.\S+/;
                if(email.value.match(ptrn)){
                alert('Valid Input');}
                else{
                alert('Invalid Input');
                document.getElementById('em').focus();
            }}
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined label ‘Enter Email ID’ using <label> tag then input tag defined with id attribute ‘em’ and last we defined submit button with onclick event for initialize emValid() function call when user submit their response.
  7. In script we defined emValid() function, here we stored user email id to variable ‘email’ and variable ‘ptrn’ holds pattern of email id’s.
  8. Using if() condition we checks whether user entered email id match with pattern or not if it is true then it display message ‘Valid Input’ on alert box otherwise it display ‘Invalid Input’.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to validate email in javascript.

When we executes our program on browser we can see one label ‘Enter Email’ with input field for user can provide their email id if they provide any invalid email id value and submitted then it will notify user to that is invalid input otherwise it will display valid input result on webpage.

We can also display error or success message on webpage instead of alert box.

I hope this tutorial on regular expression for email validation in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪