Monday, July 7, 2008

Common validation for Email in JS

//E-Mail validation
/* mail is the id of text box. The argument required means the field is necessary or not. If we send the value
of true to required, It means this is a mendatory field otherwise this field is optional */
function ValidateEmail(mail,required,spanID)
{
document.getElementById(spanID).innerHTML='';
mail=document.getElementById(mail);
var email = mail.value;
var emailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}(comnetorgeduintmilgovarpabizaeronamecoopinfopromuseum))$/;
if(required == true)
{
if(email == '' email == null email == "")
{
document.getElementById(spanID).innerHTML="Email is required";
return false;
}
else
{
if (!email.match(emailFormat))
{
document.getElementById(spanID).innerHTML="Invalid email address";
return false;
}
else
{
return true;
}
}
}
else
{
if(email == '' email == null email == "")
{
return true;
}
else
{
if (!email.match(emailFormat))
{
document.getElementById(spanID).innerHTML="Invalid Mail ID";
return false;
}
else
{
return true;
}
}
}
}

No comments: