Introduction
This article explains how to apply a single JavaScript Function to multiple Textboxes to validate them.
Our applications usually have more than five Textboxes on each web page, 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.
Its code is as follows.
<table>
<tr>
<td>
<asp:Label runat="server" ID="lbl1" Text="First Name:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtID2" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
<td>
<asp:Label runat="server" ID="Label4" Text="Middle Name:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="Label1" Text="Last Name:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
<td>
<asp:Label runat="server" ID="Label5" Text="Email ID:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="Label2" Text="Phone Number:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
<td>
<asp:Label runat="server" ID="Label6" Text="Age:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="Label3" Text="Fathers Name:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" onblur="onLeave(this)"></asp:TextBox>
</td>
<td>
<asp:Label runat="server" ID="Label7" Text="Mothers Name:-"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" Text="Submit" />
</td>
</tr>
</table>






Join the conversation! Your thoughts help the community grow.