Code below is a well tested and users can just copy & paste the same to validate emailID's using javascript.
This code also has the code to validate multiple emailIDs which are comma separated using javascript.
This code also has the code to validate multiple emailIDs which are comma separated using javascript.
Sample.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Comma Seperated EmailID validation</title>
<script type="text/javascript">
function echeck(str) {
var at="@";
var dot=".";
var lat=str.indexOf(at);
var lstr=str.length;
var ldot=str.indexOf(dot);
var str1=str.split('.')
if (str1[1]==null || str1[1].length<2){
return false;
}
if (str1[1]==null || str1.length>9){
return false;
}
if (str.indexOf(at)==-1){
return false;
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
return false;
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==--lstr){
return false;
}
if (str.indexOf(at,(lat+1))!=-1){
return false;
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
return false;
}
if (str.indexOf(dot,(lat+2))==-1){
return false;
}
if (str.indexOf(" ")!=-1){
return false;
}
if (!isProperMailId(str)){
return false;
}
return true;
}
function isProperMailId(string) {
if(!string) return false;
var iChars = "=+*|,\":<>[]{}~`\';()&$#%!^\\\/?-";
for (var i = 0; i < string.length; i++){
if(iChars.indexOf(string.charAt(i)) != -1)
return false;
}
return true;
}
function multiEmail() {
var email1 = document.getElementById('email').value;
var myarray=email1.split(",");
for(var i = 0; i < myarray.length; i++)
{
if (!echeck(myarray[i]))
{
alert('One or more email addresses entered is invalid. Kindly check & re-submit Again');
return false;
}
}
}
</script>
</head>
<body>
<form name="Form" id="Form" action="#" onsubmit="return multiEmail();">
<table class="tablebg" align="center" id="content">
<tr><th>ASD</th>
<th>EMAIL</th></tr>
<tr>
<td> </td>
<td><input type="text" name="email" id="email" maxlength="100" ></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Send Email" ></td>
</tr>
</table>
</form>
</body>
</html>
No comments:
Post a Comment