Smart JavaScript Email Validation
How to validate email address use JavaScript, What is correct Email format
Usage:
This validation is used to check whether the email address is in correct format. The correct email format is xxx@xxx.top-level-domain. xxx must be '-' or ' _' or '.' or alphanumeric characters only
compatibility: | ![]() | ![]() | ![]() | ![]() | ![]() |
JavaScript Code:
Insert this JavaScript in the head section of your html code.
<script type="text/javascript">
function validation(control_name,id){
var address = document.getElementById(control_name).value;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
switch (id){
case 1:
if(address == ''){
alert('Enter your Email Address');
document.getElementById(control_name).focus();
}else if(reg.test(address) == false) {
alert('Invalid Email');
} else {
alert('Valid Email');
}
break;
case 2:
if(address == ''){
document.getElementById('message_email').innerHTML = ('Enter your Email Address');
}else if(reg.test(address) == false) {
document.getElementById('message_email').innerHTML = ('Invalid Email');
} else {
document.getElementById('message_email').innerHTML = ('Valid Email');
}
break;
}
}
</script>
HTML Code:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>Email Address</td>
<td><input name="email_alert" id="email_alert" type="text" /></td>
<td><input name="btn_validate" type="button" value="Validate" onclick="validation('email_alert',1)" /></td>
</tr>
<tr>
<td>Email Address </td>
<td><input name="email" id="email" type="text" onkeyup="validation(this.id,2)"/></td>
<td style="color:#FF0000; font-family:Arial, Helvetica, sans-serif; font-size:10px;" id="message_email"> </td>
</tr>
</table>
</body>
</html>
Demo:
| Email Address | ||
| Email Address |





No comments:
Post a Comment