Tuesday, December 20, 2011

JavaScript Form validation


JavaScript form validation

<script language="javascript" >
 function userval()
{
  mErrStr="";
mFieldName="";
if(!addmembers())
{
  if(mErrStr!="")
{
alert("Following are the required fields :\n\n"+mErrStr);
for(i=0;i<document.adduser.elements.length;i++)
{
if(document.adduser.elements[i].name==mFieldName)
document.adduser.elements[i].focus();
}
return false;
}

return true;
}
}
function addmembers()
{
mErrStr="";
mFieldName="";
var formName=document.adduser;
if(formName.Name.value == "")
{
mErrStr +="First Name\n";
if(mFieldName == "")
mFieldName = "Name";
}
if(formName.Name.value!="")
      {
         if(formName.Name.value.length < 4)
      {
   mErrStr +="First Name must be at least 4 characters long\n";
       if(mFieldName == "")
  mFieldName = "Name";
      }
    }
  if(formName.LName.value == "")
{
mErrStr +="Last Name\n";
if(mFieldName == "")
mFieldName = "LName";
}
if(formName.LName.value!="")
      {
         if(formName.LName.value.length <4)
      {
   mErrStr +="Last Name must be at least 4 characters long\n";
       if(mFieldName == "")
  mFieldName = "LName";
      }
    }
if(formName.Email.value == "")
{
mErrStr +="Email\n";
if(mFieldName == "")
mFieldName = "Email";
}
 if(formName.Email.value!="")
      {
         var fn_reg=/^[A-Za-z]([A-Za-z0-9][.]?)+[@][A-Za-z0-9]{2,6}[.]([A-Za-z]{3}|[A-Za-z]{2}[.][A-Za-z]{2})$/;
        if(fn_reg.test(formName.Email.value)==false)
      {
   mErrStr +="Enter valid Email\n";
       if(mFieldName == "")
  mFieldName = "Email";
      }
    }
  if(formName.Contact.value == "")
{
mErrStr +="Contact No\n";
if(mFieldName == "")
mFieldName = "Contact";
}
if(formName.Contact.value!="")
      {
         var fn_reg=/^[0-9]{10}$/;
        if(fn_reg.test(formName.Contact.value)==false)
      {
   mErrStr +="Enter valid Contact No\n";
       if(mFieldName == "")
  mFieldName = "Contact";
      }
    }
if(formName.Loginname.value == "")
{
mErrStr +="Login name\n";
if(mFieldName == "")
mFieldName = "Loginname";
}
if(formName.Loginname.value!="")
      {
         var fn_reg=/^[A-Za-z0-9]{4,}$/;
        if(fn_reg.test(formName.Loginname.value)==false)
      {
   mErrStr +="Loginname must be at least 4 characters long\n";
       if(mFieldName == "")
  mFieldName = "Loginname";
      }
    }
if(formName.Password.value == "")
{
mErrStr +="Password\n";
if(mFieldName == "")
mFieldName = "Password";
}
if(formName.Password.value!="")
      {
         if(formName.Password.value.length < 6)
      {
   mErrStr +="Password must be at least 6 characters long\n";
       if(mFieldName == "")
  mFieldName = "Password";
      }
    }
if(formName.Cpassword.value == "")
{
mErrStr +="Confirm password\n";
if(mFieldName == "")
mFieldName = "Cpassword";
}
if(formName.Cpassword.value!="")
{
if(formName.Password.value!=formName.Cpassword.value)
{
mErrStr +="Password and Confirm Password do not match.\n";
if(mFieldName == "")
mFieldName = "Cpassword";
}
}
if(formName.Location.value == "")
{
mErrStr +="Location\n";
if(mFieldName == "")
mFieldName = "Location";
}
  if(formName.image.value != "")
 {
   var extension=formName.image.value.lastIndexOf(".");
   var imgtype=formName.image.value.substr(extension);
   if(imgtype!=".jpg" && imgtype!=".png" && imgtype!=".jpeg" && imgtype!=".gif")
   {
 mErrStr +="Upload valid Image.Ex:.jpg(or).jpeg(or).gif(or).png\n";
 if(mFieldName == "")
mFieldName = "image";
   }
      }

 }
 </script>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.