Friday, 24 March 2017

How to Validate Email Id Using Javascript

In this article we see how to validate email id using regex in javascript.

HTML Code:-

form name="frmSample" method="post" action="#" onSubmit="return ValidateForm()">
<p>Enter an Email Address :
 <input type="text" name="txtEmail">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>


Javascript Code:-
function ValidateForm() {
    var emailId=document.getElementsByName("txtEmail")[0].value;
    if(isValidEmailAddress(emailId)) {
        alert("Valid email id");
    } else {
        alert("Invalid email id");
    }
}


function isValidEmailAddress(email) {
    var filter=/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/;
    return filter.test(email);
}

No comments:

Post a Comment