Friday, 30 March 2012

Validations JS


 validations


//CODE FOR CLASS FILE OF JS

//FUNCTION  FOR  VALIDATE  CHARACTER TEXTBOX
 function validatecontrols(S1,msg)
 {
 var x=S1.value;
 if(x==null || x=="")
 {
  alert(msg+ 'cant empty');
  S1.value="";
  S1.focus();
 return false
 }
  if(!isNaN(x))
 {
 alert (msg+'value should be in character');
 return false
 }
 var q=/^[a-z A-Z]*$/;
 if (!x.match(q))
 {
 alert (msg+'charater enter');
 return false
 }
 }
//FUNCTION FOR VALIDATE BLANK
 function CheckBlanks(S1,msg)
 {
 var x=S1.value;
 if(x==null || x=="")
 {
  alert(msg+ 'cant empty');
  S1.value="";
  S1.focus();
 return false
 }
 }
 //FUNCTION FOR VALIDATE MOBILE
 function MobileNoValidate(S1,msg)
 {
  var c=S1.value;
 if(isNaN(c)||c.indexOf(" ")!=-1)
 {
 alert(msg+ ' should be number');
  S1.value="";
  S1.focus();
 return false
 }
 if(c.length>11 || c.length<10)
 {
 alert(msg+' should be 10 number')
 return false
 }
 if(c.charAt(0)!="9")
 {
 alert(msg+' statr should be 9 number');
 return false
 }
 }
 //FUNCTION FOR VALIDATE EMAIL ID
 function EmailValidate(S1,msg)
 {

 var w=S1.value;
 var atdot=w.indexOf("@");
 var dotat=w.lastIndexOf(".");
 if(atdot<1||dotat<atdot+2||dotat+2>=w.length)
 {
 alert(msg+'enter valid email');
  S1.value="";
  S1.focus();
 return false
 }
 var s=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 if(!w.match(s))
 {
 alert('email should be proper');
  S1.value="";
  S1.focus();
 return false
 }

 var w=S1.value;
 if(w==null || w=="")
 {
 alert('should be enter value');
  S1.value="";
  S1.focus();
 return false
 }
 }
 //FUNCTION FOR VALIDATE CONFIRM PASSWORD
function confirmpassword (s1,s2,msg)
  {
  if(s1.value!=s2.value)
  {
  alert('please enter confirmPassword');
  return false;
  }
  }
//FUNCTION FOR VALIDATE GENDER
function ValidateForGender(s1,msg)
{
  for(var i=0;i< s1.length;i++)
  {
     if(s1[i].checked)
     {
       return true;
     }
  }
  document.getElementById("rdlMF").value="";
  document.getElementById("rdlMF").focus();
  alert("Please select Gender "+msg+".");
  return false;
}


//Function  calling
    <script src="../Script/try.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">

 function validatecontrolstry()
 {
    if(CheckBlanks(document.getElementById("TextBox1"),'Name ')==false)
        return false;
        if(validatecontrols(document.getElementById("TextBox1"),' UserName ')==false)
        return false;
     
        if(MobileNoValidate(document.getElementById("TextBox2"),'MobileNo')==false)
        return false;
     
        if(EmailValidate(document.getElementById("TextBox3"),'EmailID')==false)
        return false;
       
        if(validatecontrols(document.getElementById("TextBox4"),'Password')==false)
        return false;
       
        if(validatecontrols(document.getElementById("TextBox5"),'Re-Password')==false)
        return false;
       
       if(confirmpassword(document.getElementById("TextBox4"),document.getElementById("TextBox5"),'RePassword')==false)
        return false;
      
       // Dropdown validation
        if(document.getElementById("DropDownList1").value =='---Select---')
         {
           alert('Please select .'); 
           document.getElementById("DropDownList1").focus();        
             return false;
          } 
       // Redio button validation

          if(ValidateForGender(document.getElementsByName("rdlMF"),'Gender')==false)
       {
        document.getElementById("rdlMF").style.color="red";
        return false;
       } 
       // Check box validation
       if(document.getElementById("CheckBox1").checked==false)
    {
      alert("Please select T & C");
      return false;
     }
  }
  </script>

<div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        &nbsp;<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Value="---Select---"></asp:ListItem>
        <asp:ListItem Value="1No"></asp:ListItem>
        </asp:DropDownList>
        &nbsp;<asp:RadioButtonList ID="rdlMF" runat="server"
            style="font-weight700">
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
        </asp:RadioButtonList>
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:Button ID="Button1"
          runat="server" Text="Button"
          OnClientClick="javascript:return validatecontrolstry();" />       
 </div>



No comments:

Post a Comment