JavaScript Validation For Login Form using ASP.NET

  1. <script type="text/javascript">  
  2. // Main Function for all validation  
  3.         function AllInOnevalidate()   
  4.         {  
  5.             var ValidationSummary = "";  
  6.             ValidationSummary += NameValidat();  
  7.             ValidationSummary += EmailValidat();  
  8.             ValidationSummary += PhonenoValidat();  
  9.             ValidationSummary += PasswordValidat();  
  10.             if (ValidationSummary != "")   
  11.             {  
  12.                 alert(ValidationSummary);  
  13.                 return false;  
  14.             }  
  15.             else   
  16.             {  
  17.                 alert("Submited Successfuly");  
  18.                 return true;  
  19.             }  
  20.    
  21.         }  
  22. // For Name validation  
  23.         function NameValidat()   
  24.         {  
  25.    
  26.             var userid;  
  27.             var controlId = document.getElementById("<%=txtcontct.ClientID %>");  
  28.             userid = controlId.value;  
  29.             var val;  
  30.             val = /^[0-9]+$/;  
  31.             var digits = /\d(10)/;  
  32.             if (userid == "")   
  33.             {  
  34.                 return ("PhoneNo Must Require" + "\n");  
  35.             }  
  36.             else if (val.test(userid))   
  37.             {  
  38.                 return "";  
  39.             }  
  40.    
  41.             else   
  42.             {  
  43.                 return ("PhoneNo should be only in digits" + "\n");  
  44.             }  
  45.         }  
  46. // For Email validation  
  47.         function EmailValidat()   
  48.         {  
  49.             var userid;  
  50.             var controlId = document.getElementById("<%=txtname.ClientID %>");  
  51.             userid = controlId.value;  
  52.             var val = /^[a-zA-Z ]+$/  
  53.             if (userid == "")   
  54.             {  
  55.                 return ("Name Must Require" + "\n");  
  56.             }  
  57.             else if (val.test(userid))  
  58.             {  
  59.                 return "";  
  60.             }  
  61.             else  
  62.             {  
  63.                 return ("Name accepts only spaces and charcters" + "\n");  
  64.             }  
  65.         }  
  66. // For Phone validation  
  67.         function PhonenoValidat()  
  68.         {  
  69.             var userid;  
  70.             var controlId = document.getElementById("<%=txtemail.ClientID %>");  
  71.             userid = controlId.value;  
  72.             var val = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;  
  73.             if (userid == "")  
  74.             {  
  75.                 return ("Email Id Must Require" + "\n");  
  76.             }  
  77.             else if (val.test(userid))  
  78.             {  
  79.                 return "";  
  80.             }  
  81.             else  
  82.             {  
  83.                 return ("Email should be in this form example: [email protected]" + "\n");  
  84.             }  
  85.         }  
  86. // For Password validation  
  87.         function PasswordValidat()   
  88.         {  
  89.             var password = document.getElementById('<%=txtPassword.ClientID %>').value;  
  90.             var score = 0;  
  91.    
  92.             if (password.length == 0)   
  93.             {  
  94.                 return ("Please Enter Password" + "\n");  
  95.             }  
  96.             else if (password.length < 8)   
  97.             {  
  98.                 return ("Password should be 8 chr" + "\n");  
  99.             }  
  100.             else   
  101.             {  
  102.             //for small letters  
  103.             for (var s = 0; s < password.length; ++s)  
  104.             {  
  105.                 if (password.charCodeAt(s) >= 'a'.charCodeAt(0) && password.charCodeAt(s) <= 'z'.charCodeAt(0))   
  106.                 {  
  107.                     score += 1;  
  108.                     break;  
  109.                 }  
  110.             }  
  111.             //for capital letters  
  112.             for (var s = 0; s < password.length; ++s)   
  113.             {  
  114.                 if (password.charCodeAt(s) >= 'A'.charCodeAt(0) && password.charCodeAt(s) <= 'Z'.charCodeAt(0))   
  115.                 {  
  116.                     score += 1;  
  117.                     break;  
  118.                 }  
  119.             }  
  120.             //for numbers  
  121.             for (var s = 0; s < password.length; ++s)   
  122.             {  
  123.                if (password.charCodeAt(s) >= '0'.charCodeAt(0) && password.charCodeAt(s) <= '9'.charCodeAt(0))   
  124.                {  
  125.                     score += 1;  
  126.                     break;  
  127.                 }  
  128.             }  
  129.             if (score==3)  
  130.             {  
  131.                 return "";  
  132.                   
  133.             }  
  134.             else  
  135.             {  
  136.                  return ("Password should be Small Latter,Capital Latter and Digit" + "\n");  
  137.             }   
  138.         };  
  139.   
  140. </script>  
  141.    
  142. //add control for design view  
  143.    
  144. <table style="border-color: #333300; z-index: 1; left: 15px; top: 54px; position: absolute;  
  145.             height: 122px; width: 361px">  
  146.     <tr>  
  147.         <td class="style2">  
  148.             <asp:Label ID="lblname" runat="server" Text="Full Name"></asp:Label>  
  149.         </td>  
  150.         <td>  
  151.             <asp:TextBox ID="txtname" runat="server"></asp:TextBox>  
  152.         </td>  
  153.     </tr>  
  154.     <tr>  
  155.         <td class="style2">  
  156.             <asp:Label ID="lblemail" runat="server" Text="Email"></asp:Label>  
  157.         </td>  
  158.         <td>  
  159.             <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>  
  160.         </td>  
  161.     </tr>  
  162.     <tr>  
  163.         <td class="style2">  
  164.             <asp:Label ID="lblCntno" runat="server" Text="Phone No"></asp:Label>  
  165.         </td>  
  166.         <td>  
  167.             <asp:TextBox ID="txtcontct" runat="server"></asp:TextBox>  
  168.         </td>  
  169.     </tr>  
  170.     <tr>  
  171.         <td class="style2">  
  172.                     Password  
  173.                 </td>  
  174.         <td>  
  175.             <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>  
  176.         </td>  
  177.     </tr>  
  178.     <tr>  
  179.         <td class="style3"></td>  
  180.         <td class="style1">  
  181.             <asp:Button ID="bttnsubmit" runat="server" Font-Bold="True" OnClientClick="javascript:AllInOnevalidate()"  
  182.                         Text="Submit" onclick="bttnsubmit_Click" />  
  183.         </td>  
  184.     </tr>  
  185. </table>