validating email address without regular expression in javascript

validate_email_address

Here in this post we will validate email address without regular expression, though we can perform a very basic validation of email address with JavaScript by implementing the following three email validation rules.

1. The email address must have @ character
2. The email address must have .(dot) character
3. There must be at lease 2 characters between @ and .(dot)

JavaScript Function for this

    function validate(){
        var t=document.getElementById('email');
        var x=t.value.indexOf('@');
        var y=t.value.lastIndexOf('.');

        if(x==-1 || y==-1 || (x+2)>=y){
            alert('Email address is not valid');
        }
        else{
            alert('Email is ok');
        }
    }

HTML Page

<title>Email validation with JavaScript</title>
</head>
<body>
     <input type="text" name="email" id="email" />
     <input type="button" value="Validate" onclick="validate()" />
</body>
</html>

There are many techniques of validating email address, each validation method has its own pros and cons. The above method doesn't require understanding of regular expressions

Thanks

Print Friendly

Leave a Reply

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