Tuesday, July 1, 2008

Validate Multiple Email Addresses in javascript

function mailValidation()
{
var emailfield = document.getElementById('controlId').value;
var email = emailfield.split(';');
if(email.length > 0)
{
document.getElementById('spanId').innerHTML="";
for(var i=0;i < email.length;i++)
{
if(!validateEmails(email[i],1,1,'spanId','controlId'))
{
alert("Test");
}
var toSpan = document.getElementById('spanId').innerHTML;

}
if(toSpan != "")
{
return false;
}
}
}
function validateEmails(addr,man,db,SpanId,controlId)
{
if (addr == '' && man) {
if (db)
document.getElementById(SpanId).innerHTML="Enter To address ";
document.getElementById(controlId).focus();
return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ",:?!()[]\{\}^|';
for (i=0; i if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
if (db)
document.getElementById(SpanId).innerHTML="To address contains invalid characters(Enter in the format:name@example.com;name@example.com)";
document.getElementById(controlId).focus();
return false;
}
}
for (i=0; i if (addr.charCodeAt(i)>127) {
if (db)
document.getElementById(SpanId).innerHTML="To address contains non ascii characters";
document.getElementById(controlId).focus();
return false;
}
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
if (db)
document.getElementById(SpanId).innerHTML="Email address must contain an @";
document.getElementById(controlId).focus();
return false;
}
if (atPos == 0) {
if (db)
document.getElementById(SpanId).innerHTML="Email address must not start with @";
document.getElementById(controlId).focus();
return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
if (db)
document.getElementById(SpanId).innerHTML="Email address must contain only one @";
document.getElementById(controlId).focus();
return false;
}
if (addr.indexOf('.', atPos) == -1) {
if (db)
document.getElementById(SpanId).innerHTML="Email address must contain a period in the domain name";
document.getElementById(controlId).focus();
return false;
}
if (addr.indexOf('@.',0) != -1) {
if (db)
document.getElementById(SpanId).innerHTML="period must not immediately follow @ in email address";
document.getElementById(controlId).focus();
return false;
}
if (addr.indexOf('.@',0) != -1){
if (db)
document.getElementById(SpanId).innerHTML="period must not immediately precede @ in email address";
document.getElementById(controlId).focus();
return false;
}
if (addr.indexOf('..',0) != -1) {
if (db)
document.getElementById(SpanId).innerHTML="Two periods must not be adjacent in email address";
document.getElementById(controlId).focus();
return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
if (db)
document.getElementById(SpanId).innerHTML="Invalid primary domain in email address";
document.getElementById(controlId).focus();
return false;
}

}

No comments: