HOW TO VALIDATE EMAIL ADDRESS IN JAVASCRIPT.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 648  View(s)
Ratings:
Rate this:
I want to to Validate Email address in javascript. How to Solve this problem please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to Validate Emailaddress in javascript.

    Javascript

        <script type="text/javascript">      
            $(document).ready(function (e) {
                function CheckEmail(email) {
                    var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                    if (reg.test(email)) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                $('#btnok').click(function () {
                    debugger;
                    var email = $('#txtemail').val();
                    if ($.trim(email).length == 0) {
                        alert('Please Enter Valid Email Address');
                        return false;
                    }
                    if (CheckEmail(email)) {
                        alert('Valid Email Address');
                        return false;
                    }
                    else {
                        alert('Invalid Email Address');
                        return false;
                    }
                });
            });
    </script>

    HTML

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="Scripts/jquery-2.1.4.js"></script>
        <script type="text/javascript">      
            $(document).ready(function (e) {
                function CheckEmail(email) {
                    var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                    if (reg.test(email)) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
                $('#btnok').click(function () {
                    debugger;
                    var email = $('#txtemail').val();
                    if ($.trim(email).length == 0) {
                        alert('Please Enter Valid Email Address');
                        return false;
                    }
                    if (CheckEmail(email)) {
                        alert('Valid Email Address');
                        return false;
                    }
                    else {
                        alert('Invalid Email Address');
                        return false;
                    }
                });
            });
    </script>
    </head>
    <body>
    Email Address: <input type="text" id="txtemail"/><br/>
    <input type="submit" id="btnok" Value="Submit" />
    </body>
    </html>

     

      Modified On Jan-27-2016 05:55:37 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!