Email Address Validation in JavaScript

Introduction: Hello Guys, “Welcome back” Today i am here with another one new great article. In this article I will explain about how implement the validate email address in javascript using Regular Expression. 

For more Update Please follow us on our  Whtsapp Channel

For more Update Please follow us on our  Facebook Page

 Code  for HTML Page

On the HTML page there will be Two Controls, one is text box with the id named text_email for to enter the input by the user and other control is Button with the id named button_validate. On the The Button click I assigned a javascript  function to the onclick event handler.

Email id:

<input type="text" id="text_email" />

<br /><br />

<input type="button" id="button_validate" value="Validate Email" onclick="Validate_email()" />

 

 

When user will click on the Validate button then javascript function will be called.

 

IsValidEmail

On the HTML page I will use javascript function named check_validemail, email entered in the TextBox will be send as parameter and it is validated using the Regular Expression and It will be returned the values as TRUE or False. If the user entered the correct or valid email address then it will pass TRUE and on the other hand if user entered the incorrect or invalid email address then it will pass FALSE.

At the end, there will be message or alert displayed on the screen using alert Message box.

<script type="text/javascript">

    function IsValidEmail(email) {

        var  expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

        return expr.test(email);

    };

    function validate_email() {

        var email = document.getElementById("text_email").value;

        if (IsValidEmail(email)) {

            alert("Valid email address.");

        }

        else {

            alert("Invalid email address.");

        }

    }

</script>

 

Now here i am giving complete code for the HTML page. You can copy and paste the whole code on your HTML page. And enjoy the execution of the whole concept.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

     <title></title>

    <style type="text/css">

        body { font-family: Arial; font-size: 10pt; }

    </style>

    <script type="text/javascript">

        function IsValidEmail(email) {

            var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

            return expr.test(email);

        };

        function Validate_Email() {

            var email = document.getElementById("text_email").value;

            if (IsValidEmail(email)) {

                alert("Valid email address.");

            }

            else {

                alert("Invalid email address.");

            }

        }

    </script>

</head>

<body>

  

        Email Address:

        <input type="text" id="text_email" />

        <br /><br />

        <input type="button" id="button_Validate" value="Validate Email" onclick="Validate_Email()" />

   

</body>   

 

</html>

 

Conclusion: In above code, I explained that about how to implement the validate email address with javascript using Regular Expression. This code is very helpful for every developer. Bye Bye and take care of you all Developers. We will come back shortly with the new article.

Regards

Using Asp.net

 

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net