Email validation in JavaScript

The below JavaScript code validates a textbox for correct email id. Whatever user enters if not a proper email id then it will display a message in the alert box.

<html>
<p>JavaScript</p>
<body>
<script type='text/javascript'>
function emailValidation()
{
var emailIdValue= /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(document.getElementById('txtEmail').value.match(emailIdValue))
{
alert("Email Validation sucessfully");
return true;
}
else
{
alert("This email is invalid, give a proper email id");
txtEmail.focus();
return false;
}
}
</script>
<form>
Enter the E-mail Address value: <input type='text' id='txtEmail'/>
<input type='button' onclick="emailValidation()" id="btnSubmit" value='Submit' />
</form>
</body>
</html>

Leave a Reply

Your email address will not be published.