Apply Single JavaScript Function to Multiple Textboxes For Validation

Introduction

 
This article explains how to apply a single JavaScript Function to multiple Textboxes to validate them.
 
Our applications usually have more then five Textboxes on each Webpage and if it's a form then the number of Textboxes can increase up to 20-30, in that case creating a JavaScript Function for each TextBox is not a good choice because it will create a heavy code only for validations. This article will help you create only a single JavaScript Function that can be applied to any number of Textboxes.
 
Use the following procedure to create such an application.
 
Step 1
 
First of all, I will create a new application in which I will create a form to be submitted by the user.
 
It's code is as follows:
  1. <table>  
  2.      <tr>  
  3.           <td>  
  4.                <asp:Label runat="server" ID="lbl1" Text="First Name:-"></asp:Label>  
  5.           </td>  
  6.           <td>  
  7.                <asp:TextBox ID="txtID2" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  8.           </td>  
  9.           <td>  
  10.                <asp:Label runat="server" ID="Label4" Text="Middle Name:-"></asp:Label>  
  11.           </td>  
  12.           <td>  
  13.                <asp:TextBox ID="TextBox4" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  14.           </td>  
  15.      </tr>  
  16.      <tr>  
  17.           <td>  
  18.                <asp:Label runat="server" ID="Label1" Text="Last Name:-"></asp:Label>  
  19.           </td>  
  20.           <td>  
  21.                <asp:TextBox ID="TextBox1" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  22.           </td>  
  23.           <td>  
  24.                <asp:Label runat="server" ID="Label5" Text="Email ID:-"></asp:Label>  
  25.           </td>  
  26.           <td>  
  27.                <asp:TextBox ID="TextBox5" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  28.           </td>  
  29.      </tr>  
  30.      <tr>  
  31.           <td>  
  32.                <asp:Label runat="server" ID="Label2" Text="Phone Number:-"></asp:Label>  
  33.           </td>  
  34.           <td>  
  35.                <asp:TextBox ID="TextBox2" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  36.           </td>  
  37.           <td>  
  38.                <asp:Label runat="server" ID="Label6" Text="Age:-"></asp:Label>  
  39.           </td>  
  40.           <td>  
  41.                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>  
  42.           </td>  
  43.      </tr>  
  44.      <tr>  
  45.           <td>  
  46.                <asp:Label runat="server" ID="Label3" Text="Fathers Name:-"></asp:Label>  
  47.           </td>  
  48.           <td>  
  49.                <asp:TextBox ID="TextBox3" runat="server" onblur="onLeave(this)"></asp:TextBox>  
  50.           </td>  
  51.           <td>  
  52.                <asp:Label runat="server" ID="Label7" Text="Mothers Name:-"></asp:Label>  
  53.           </td>  
  54.           <td>  
  55.                <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>  
  56.           </td>  
  57.      </tr>  
  58.      <tr>  
  59.           <td>  
  60.                <asp:Button runat="server" Text="Submit" />  
  61.           </td>  
  62.      </tr>  
  63. </table> 
Step 2
 
Now I will create the JavaScript function that is to be applied for validating the Textboxes.
  1. <script>  
  2. function onLeave(_input) {  
  3.     var check = _input.value;  
  4.     if (check.length < 3) {  
  5.         alert("Enter Something");  
  6.         _input.focus();  
  7.     }  
  8. }  
  9. </script> 
Here _input represents the Textboxes. A variable named check is used that will hold the value of the TextBox on which it is applied.
 
Then the Length of that value will be checked, if the length is found to be less than 3 then an alert message will be shown and the focus will be returned to the TextBox of which the user was earlier.
 
Step 3
 
After creating the Function I call it on the Blur event of the TextBox. It's done like this:
  1. onblur="onLeave(this)" 
Now our application is created and is ready to get executed.
 
Output
 
On running the application the user will see a form to be filled.
 
one javascript function for all textboxes
 
Now I clicked on the first TextBox.
 
one javascript function for all textboxes
 
But I tried to leave this TextBox and go to the other one but as you can see I was not allowed to do that, an error message was displayed and the focus will again go to the first TextBox
 
one javascript function for all textboxes
 
I then provided my first name and you can see that I was allowed to go to the next TextBox.
 
one javascript function for all textboxes
 
Here also I tried to provide fewer characters than allowed and you can see again the same error message is displayed and the focus will go to the second TextBox.
 
one javascript function for all textboxes