Background
In my previous article, we learned how to do validation Using JavaScript with a basic introduction. There are many disadvantages of using JavaScript that we will discuss later. In this article, we will learn how to do ASP.Net form validation using jQuery.
If you are a beginner or student then please read my previous article.
ASP.Net Form Validation Using JavaScript
I hope you have read the preceding article. Let us now start with the creation of the function in jQuery.
Note
- To use jQuery in an ASP.Net form you need to add the reference for the jQuery library which is nothing but a JavaScript file
- You can download the latest jQuery library from http://jquery.com/download/
Creating the function in jQuery
The function is created in jQuery using the following syntax.
Syntax
$(document).ready(function () {
$('#btnSave').click(function () {
})
});
In the preceding syntax, the function is the keyword provided by jQuery to declare a function followed by a "$" (dollar) symbol, and the "$('#btnSave').click" means that this function is called on the ASP.Net button click that has the id "btnSave".
Example
$(document).ready(function () {
$('#btnSave').click(function () {
alert("button is clicked")
})
});
I hope you now understand the basics of validation in jQuery. Let us now create one sample web application that demonstrates how to do validation.
Let us first create the web application with two web pages as in the following.
- "Start", "All Programs", "Microsoft Visual Studio 2010"
- "File", "New Website", "C# - Empty website" (to avoid adding a master page)
- Give the website a name, such as Validation or whatever you wish, and specify the location
- Then right-click on the solution in the Solution Explorer then select "Add New Item" - "Default.aspx page" (add two pages).
We are adding two web pages because our requirement is, that on the first web page, there is form data to be filled in by the user and only after validating the form data, the form will be redirected to the next page.
The first-page source code <body> tag will look as in the following.
<body bgcolor="#3366ff">
<form id="form2" runat="server">
<br />
<br />
<div>
<table>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email Id
</td>
<td>
<asp:TextBox ID="txtmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gender
</td>
<td>
<asp:DropDownList ID="ddlType" runat="server">
<asp:ListItem Value="0">-Select-</asp:ListItem>
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
word
</td>
<td>
<asp:TextBox ID="txt1" runat="server" TextMode="word"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Confirm word
</td>
<td>
<asp:TextBox ID="txt2" runat="server" TextMode="word"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSave" runat="server" Text="Create" />
<asp:Button ID="Button1" runat="server" Text="Reset" />
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
Look at the preceding source code closely; see the IDs of the controls that play an important role in JavaScript validation by reading the ASP.Net control values in JavaScript code.
The design view of the preceding source code will look as in the following.

I hope you have created the same form for demonstration purposes as above.
Reading ASP.Net Control values in jQuery
We can read ASP.Net control values in jQuery using the following syntax starting form the "$" symbol followed by the "#" symbol and control ID as in the following.
$("#txtUserId").val(
How to add the .js file?
Right-click on Solution Explorer then select "Add New Item" Then in the "script.js" page rename the .js page as you wish, I have renamed it to "UserValidation.js".
Now declare the variable inside the function using the var keyword to read the ASP.Net control values by their IDs and assign the values to the variable as.






Arunprasad T S VPosted Jun 27, 2018, 10:23 AM
<script src="jquery-2.0.3.js" type="text/javascript"></script> you didn't tell about this line. i got error : file jquery-2.0.3.js was not found
Arunprasad T S VPosted Jun 27, 2018, 9:43 AM
Hi, I got confused. first you should create validation for one textbox, please. Kindly share the code.
Pappu KumarPosted Dec 17, 2015, 7:06 AM
..........................
Vithal WadjePosted Sep 27, 2014, 9:35 AM
yes right,server side validation must but client side validation is also important
Ismail Hakki SenPosted Sep 27, 2014, 7:28 AM
this can be use but need to server-side validation. thanks.
Vithal WadjePosted Sep 1, 2013, 3:10 PM
thanks ali sir
ali khanPosted Sep 1, 2013, 3:01 PM
very useful
Vithal WadjePosted Aug 30, 2013, 4:40 PM
right gakenh01 sir.. ,....next....article will be on that after typeScript.thanks for reading and your valuable suggestion
Gakenh01Posted Aug 30, 2013, 11:52 AM
I must be getting old, but using ASP.Net validation controls then in your code behind do a if(Page.IsValid) is the best way to handle client and then server validation. This article reminded me of classic ASP validation. I'll look forward to future articles.
Vithal WadjePosted Aug 30, 2013, 12:40 AM
yes Deepak Kumar Verma sir.i knows that .that's why I am writing series of articles on validation by comparing a disadvantages scripting languages ,after another three article i will write secure validation techniques but don't forget client side validation techniques has huge advantages wait for my next articles,thanks
Deepak VermaPosted Aug 29, 2013, 11:17 PM
Good for beginners. But JavaScript / jQuery validations can be easily bypassed, if end-user has disabled JavaScript in the browser.