Validations In JavaScript

Introduction

 
As we know JavaScript is used for mainly client-side validations. Here in this article, I have completed almost all JavaScript validations that we need during our project. 
 

Textbox should not be blank

 
Here in this  demo, I will explain how to restrict a user to fill a textbox using javascript. Initially I have the form like this.
 
 
Here is my Html code for this.
  1. <body>    
  2.     
  3.     <form id="form1" runat="server">    
  4.         <div style="margin-left:200px; border:solid; border-color:aqua; width:300px;">    
  5.             <table>    
  6.                 <tr>    
  7.                     <td>    
  8.                         <b>Name</b>    
  9.                     </td>    
  10.                 </tr>    
  11.                 <tr>    
  12.                     <td>    
  13.                         <asp:TextBox ID="txt_name" runat="server" Width="183px"></asp:TextBox>    
  14.                     </td>    
  15.                 </tr>    
  16.                 <tr>    
  17.                     <td>    
  18.                         <b>Select Country:-</b>    
  19.                     </td>    
  20.                 </tr>    
  21.                 <tr>    
  22.                     <td>    
  23.                         <asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="191px">    
  24.                             <asp:ListItem>--Select--</asp:ListItem>    
  25.                             <asp:ListItem>India</asp:ListItem>    
  26.                             <asp:ListItem>Pakistan</asp:ListItem>    
  27.                             <asp:ListItem>Kenya</asp:ListItem>    
  28.                             <asp:ListItem>SriLanka</asp:ListItem>    
  29.                         </asp:DropDownList>    
  30.                     </td>    
  31.                 </tr>    
  32.                 <tr>    
  33.                     <td>    
  34.                         <asp:Button runat="server" ID="btn_save" Text="Add" OnClientClick="return validate()" />    
  35.                     </td>    
  36.                 </tr>    
  37.             </table>    
  38.         </div>   
    Here is the script to add validations: 
    1. <head runat="server">    
    2.     <title></title>    
    3.     <script type="text/javascript">    
    4.         function validate() {    
    5.             if (document.getElementById("<%=txt_name.ClientID%>").value == "") {    
    6.                 alert('!!!!!!! Name should not be blank');    
    7.                 document.getElementById("<%=txt_name.ClientID%>").focus();    
    8.                 return false;    
    9.             }    
    10.             return true;    
    11.         }    
    12.     </script>    
    13. </head>   
      So it will popup the following message when user clicks the button without entering a username.
       
       

      Dropdown list should not be blank

       
      In this, I have explained how to restrict a user to select the dropdownlist.
       
      Here I have the same for:
       
       
      Here's the code for my DropDownList. 
      1. <tr>    
      2.     <td>    
      3.         <b>Select Country:-</b>    
      4.     </td>    
      5. </tr>    
      6. <tr>    
      7.     <td>    
      8.         <asp:DropDownList ID="ddl_country" runat="server" Height="16px" Width="191px">    
      9.             <asp:ListItem>--Select--</asp:ListItem>    
      10.             <asp:ListItem>India</asp:ListItem>    
      11.             <asp:ListItem>Pakistan</asp:ListItem>    
      12.             <asp:ListItem>Kenya</asp:ListItem>    
      13.             <asp:ListItem>SriLanka</asp:ListItem>    
      14.         </asp:DropDownList>    
      15.     </td>    
      16. </tr>    
      17. <tr>    
      18.     <td>    
      19.         <asp:Button runat="server" ID="btn_save" Text="Add" OnClientClick="return validate()" />    
      20.     </td>    
      21. </tr>    
        And here is my javascript function to validate the dropdown. 
        1. <script type="text/javascript">    
        2.     function validate() {    
        3.         if (document.getElementById("<%=ddl_country.ClientID%>").value == "--Select--") {    
        4.             alert('!!!!!!Please Enter a country.');    
        5.             document.getElementById("<%=ddl_country.ClientID%>").focus();    
        6.             return false;    
        7.         }    
        8.         return true;    
        9.     }    
        10. </script>   
          Here this is how you get the error message on clicking the button without selecting the DropDownList.
           
           

          Allow the Only character in a TextBox

           
          Please design the form with a textbox as in the following screenshot:
           
           
          Here is my html code. 
          1. <body>    
          2.     <form id="form1" runat="server">    
          3.         <div style="margin-left:200px; border:solid; border-color:aqua; width:300px;">    
          4.             <table>    
          5.                 <tr>    
          6.                     <td>    
          7.                         <b>Name</b>    
          8.                     </td>    
          9.                 </tr>    
          10.                 <tr>    
          11.                     <td>    
          12.                         <asp:TextBox ID="txt_name" runat="server" Width="183px" onkeyup=" Ischar (this)"></asp:TextBox>    
          13.                     </td>    
          14.                 </tr>    
          15.                 <tr>    
          16.                     <td>    
          17.                         <asp:Button runat="server" ID="btn_save" Text="Add" />    
          18.                     </td>    
          19.                 </tr>    
          20.             </table>    
          21.         </div>    
          22.     </form>    
          23. </body>    
            And here is my javascript to validate the textbox. 
            1. <script type="text/javascript">    
            2.     function Ischar(field) {    
            3.         var re = /^[’A-Z’’a-z’]*$/;    
            4.         if (!re.test(field.value)) {    
            5.             alert('Only Character Values');    
            6.             field.value = field.value.replace(/[^’A-Z’’a-z’]/g, "");    
            7.             return false;    
            8.         }    
            9.         return true;    
            10.     }    
            11. </script>   
              I will get the following error message like this if I entered any number to the textbox.
               
               

              Allow Only Numeric In TextBox

               
              I have the same source here.
               
              1. <body>    
              2.     <form id="form1" runat="server">    
              3.         <div style="margin-left:200px; border:solid; border-color:aqua; width:300px;">    
              4.             <table>    
              5.                 <tr>    
              6.                     <td>    
              7.                         <b>Name</b>    
              8.                     </td>    
              9.                 </tr>    
              10.                 <tr>    
              11.                     <td>    
              12.                         <asp:TextBox ID="txt_name" runat="server" Width="183px" onkeyup=" Isnumber (this)"></asp:TextBox>    
              13.                     </td>    
              14.                 </tr>    
              15.                 <tr>    
              16.                     <td>    
              17.                         <asp:Button runat="server" ID="btn_save" Text="Add" />    
              18.                     </td>    
              19.                 </tr>    
              20.             </table>    
              21.         </div>    
              22.     </form>    
              23. </body>    
                And here is my JavaScript. 
                1. <script type="text/javascript">    
                2.     function Isnumber(field) {    
                3.         var re = /^[’0-9’’0-9’]*$/;    
                4.         if (!re.test(field.value)) {    
                5.             alert('Only numeric Values');    
                6.             field.value = field.value.replace(/[^’0-9’’0-9’]/g, "");    
                7.             return false;    
                8.         }    
                9.         return true;    
                10.     }    
                11. </script>   
                It  will give the following error message when you enter any character.
                 
                 

                Phone number validation

                 
                Here is my source code for mobile number validation. 
                1. <body>    
                2.     <form id="form1" runat="server">    
                3.         <div style="margin-left:200px; border:solid; border-color:aqua; width:300px;">    
                4.             <table>    
                5.                 <tr>    
                6.                     <td>    
                7.                         <b>Name</b>    
                8.                     </td>    
                9.                 </tr>    
                10.                 <tr>    
                11.                     <td>    
                12.                         <asp:TextBox ID="txtphone" runat="server" Width="183px"></asp:TextBox>    
                13.                     </td>    
                14.                 </tr>    
                15.                 <tr>    
                16.                     <td>    
                17.                         <asp:Button runat="server" ID="btn_save" Text="Add" OnClientClick="return validate()" />    
                18.                     </td>    
                19.                 </tr>    
                20.             </table>    
                21.         </div>    
                22.     </form>    
                23. </body>    
                  The design is like the following screenshot:
                   
                   
                  Now my JavaScript for validation is. 
                  1. <script type="text/javascript">    
                  2.     function validate() {    
                  3.         var a = document.getElementById("<%=txtphone.ClientID %>").value;    
                  4.         if (a == "Ex: 08018070777") {    
                  5.             alert("Mobile no Should Not be Blank");    
                  6.             document.getElementById("<%=txtphone.ClientID %>").focus();    
                  7.             return false;    
                  8.         }    
                  9.         if (isNaN(a)) {    
                  10.             alert("Enter the valid Mobile Number(Like : 08018070777)");    
                  11.             document.getElementById("<%=txtphone.ClientID %>").focus();    
                  12.             return false;    
                  13.         }    
                  14.         if (a.length < 11) {    
                  15.             alert(" Your Mobile Number must 11 Integers(Like :08018070777)");    
                  16.             document.getElementById("<%=txtphone.ClientID %>").focus();    
                  17.             return false;    
                  18.         }    
                  19.     }    
                  20. </script>   
                    It will give the following error if the textbox is blank.
                     
                     
                     It will give this error if your number is less than 11 digits.
                     
                     
                    It will give this if you enter any character.
                     
                     

                    Email Id Validation

                     
                    For email validation this is my html code. 
                    1. <body>    
                    2.     <form id="form1" runat="server">    
                    3.         <div style="margin-left:200px; border:solid; border-color:aqua; width:300px;">    
                    4.             <table>    
                    5.                 <tr>    
                    6.                     <td>    
                    7.                         <b>Email</b>    
                    8.                     </td>    
                    9.                 </tr>    
                    10.                 <tr>    
                    11.                     <td>    
                    12.                         <asp:TextBox ID="TxtEmail" runat="server" Width="183px"></asp:TextBox>    
                    13.                     </td>    
                    14.                 </tr>    
                    15.                 <tr>    
                    16.                     <td>    
                    17.                         <asp:Button runat="server" ID="btn_save" Text="Add" OnClientClick="return validate()" />    
                    18.                     </td>    
                    19.                 </tr>    
                    20.             </table>    
                    21.         </div>    
                    22.     </form>    
                    23. </body>    
                      And here is my JavaScript for email validation.  
                      1. <script type="text/javascript">    
                      2.     function validate() {    
                      3.         if (document.getElementById("<%=TxtEmail.ClientID%>").value == "") {    
                      4.             alert("Email id can not be blank");    
                      5.             document.getElementById("<%=TxtEmail.ClientID%>").focus();    
                      6.             return false;    
                      7.         }    
                      8.         var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;    
                      9.         var emailid = document.getElementById("<%=TxtEmail.ClientID%>").value;    
                      10.         var matchArray = emailid.match(emailPat);    
                      11.         if (matchArray == null) {    
                      12.             alert("Your email address seems incorrect. Please try again.");    
                      13.             document.getElementById("<%=TxtEmail.ClientID%>").focus();    
                      14.             return false;    
                      15.         }    
                      16.     }    
                      17. </script>    
                        You will get the following error if you enter wrong email id
                         
                         
                         
                        Here's an error you will get if you entered nothing:
                         
                         
                        Thus in this way, we can implement all JavaScript validations.
                         

                        Summary

                         
                        These were  some of the commonly used validations which I documented so that whenever I need it in the project I can use without searching over the Internet. Hope this will help everyone.


                        Similar Articles