How to validate an email address using JavaScript?
Following example shows you how to validate an email address in javascript as a client side validation.
We need to validate email address very often. When you register in a web site it prompt your email address. An email address has a predefined format with it. If you entered a email address that not match the pattern, the web application shows you a message that please enter a valid email address. In most cases they use javascript validation as primary validation in forms. You can use Regular Expression to validate the email address. The pattern check the your input is matched email address or not. If it is not a valid email address It indicates that your input is incorrect and re-enter it.
E-mail address have two parts (someone@example.com). The part before the @ sign is the local-part of the address, often the username of the recipient (someone), and the part after the @ sign is the domain which is a hostname to which the e-mail message will be sent. every valid email address must contain username, @ sign and domain name.
Demo- How to validate an email address using JavaScript.
Try it!
1. Example code for validate an email address using JavaScript.
JavaScript Code
<script type="text/javascript" language="javascript">
<!--
function validateEmailAddress(){
var emailAddress = document.getElementById("emailAddress").value;
var regExEmail = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
if(emailAddress==''){
alert("Please enter a valid email address");
document.getElementById("emailAddress").focus();
return false;
}else{
if(!regExEmail.test(emailAddress)){
alert("Please enter a valid email address");
document.getElementById("emailAddress").focus();
return false;
}
}
alert("OK\nYour email address is in correct format");
return true;
}
-->
</script>
Html Code
<html>
<head>
<title>How to validate email address in JavaScript </title>
</head>
<body>
<table border="0">
<tr>
<td valign="top"> 1.Enter valid email address. </td>
<td align="left">
<input name="emailAddress" type="text" id="emailAddress" size="40" />
</td>
<td align="left"> </td>
</tr>
<tr>
<td align="right"> </td>
<td align="left" >
<input name="btnEmailAddressValidator" type="submit" id="btnEmailAddressValidator" value="Validate" onclick="javascript:validateEmailAddress();" />
</td>
<td align="left"> </td>
</tr>
</table>
</body>
</html>
©-Copyright By Duminda Chamara
JavaScript Validation
0 comments:
Post a Comment