.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> JQuery >> Post New Resource Bookmark and Share

 Subscribe to Articles

Email, UserName, Password, URL Validation using JQuery

Posted By :Pankaj Mishra      Posted Date :18/07/2009   Points :25   Category: JQuery    URL: http://www.dotnetspark.com

Email, UserName, password, URL Validation using JQuery in Asp.net. In this Article we will see Some useful validation Using JQuery in asp.net using Jquery Validation Plug-in must be useful for Login page or User Registration page etc
 


Email, UserName, password, URL Validation using JQuery in Asp.net.

In this Article we will see Some useful validation Using JQuery in asp.net using Jquery Validation Plug-in must be useful for Login page or User Registration page etc..
We will also see how to perform validation in a asp.net web page which uses master page.

As we know that JQuery is a javascript library and using thins we can able to write some like animation, Validation using minimum code.

Lets Start with first with a page which does not uses Master Page and later in this article we will see JQuery Validation with Master Page.

Step 1 : Download the JQuery Plug in from Here and JQuery Validation Script Plug-in From here both are .js file.

Step 2 : Add the above file reference in your asp.net web page under <Head>
</Head> Page
 Eg.

<script type="text/javascript" src="script/jquery-1.3.2.js"></script>
<script type="text/javascript" src="script/jquery.validate.js"></script>


Step 3: Declare asp.net text box in your form

<asp:TextBox ID="txtUserName" runat="server" Width="230px"></asp:TextBox>

Step 4: Now add you Jquery validation code inside <Script></Script> tag under <Head></Head> tag.

For eg in this we have taken a text box with id txtUserName and form name form1

<script type="text/javascript">
$(document).ready(function() {
"#form1").validate({
true
}, 
messages: {
<%=txtUserName.UniqueID %>:{ 
required: "Plaese Enter your username", 
"User name must be atleaet of 5 characters" 
</script>
 

Now we will see one by one Email, UserName, password, URL Validation using JQuery in Asp.net.

UserName Validation Using JQuery

below is the JQuery Script to validate User Name whhich is required field and Accept minimum 5 Characters

<script type="text/javascript">
$(document).ready(function() {
"#form1").validate({
true
}
}, messages: {
<%=txtUserName.UniqueID %>:{ 
required: "Plaese Enter your username", 
"User name must be atleaet of 5 characters" 
</script>


and HTML Code for the UserName Textbox

<asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>

Password Require field Validation using JQuery

Below is JQuery code to validate Password Field which is required field and should have minimum 5 characters

<script type="text/javascript">
$(document).ready(function() {
"#form1").validate({
true
}
}, messages: {
<%=txtPassword.UniqueID %>:{ 
required: "Plaese Enter your password", 
"Password must be atleaet of 5 characters" 
</script>

and the HTML code for the Password Field which is required field and should have at least 5 characters

 

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="230px"></asp:TextBox>

Email Validation Using JQuery

here is the script to validate Email using JQuery

<script type="text/javascript">
$(document).ready(function() {
"#form1").validate({
true
}
}, messages: {
<%=txtEmail.UniqueID %>:{ 
required: "Plaese Enter your Email Id", 
</script>

And HTML code for the input TextBox


<asp:TextBox ID="txtEmail" CssClass="email" runat="server" Width="230px"></asp:TextBox>

Note: Here you have to set CssClass properties to "email" to validate Email Format other wise it will not validate email format.


URL Validation using JQuery

Below is Script to validate URL of website using JQuery

<script type="text/javascript">
$(document).ready(function() {
"#form1").validate({
true
}
}, messages: {
<%=txtURL.UniqueID %>:{ 
required: "Plaese Enter Website URL", 
</script>

and the HTML code for the input Textbox for the URL

<asp:TextBox ID="txtURL"runat="server" CssClass="url" ></asp:TextBox>

Note: Here you have to set CssClass properties to "url" to validate URL Format other wise it will not validate URL format.


JQuery Validation using Master Page

Now if you have Master page and than another webpage which consume master page than just add the reference of the script inside Head tag and in a webpage say you have
Registar.aspx than go to registar.aspx page and inside the content Tag you can Write your script tag this is the only difference of implementation of JQuery Validation for with and without master page for eg

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
$(document).ready(function() {
"#aspnetForm").validate({
true
}
}, messages: {
<%=txtUserName.UniqueID %>:{ 
required: "Plaese Enter your username", 
"User name must be atleaet of 5 characters" 
</script>
<table style="width:100%;">
<tr>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
</asp:Content>


Error Message Decoration

To Decorate error message generated by Jquey you can use css like this and you can add this your custom css file you inside Head Tag

<style type="text/css">
label.error { color: red; vertical-align: top; }
</style>


What is the Difference Between Asp.Net Validation control and jQuery Validation and which one good.

Well the difference is Asp.net validation control will execute both in Client and server side if javascript is disabled than validation control will fired in server side where as JQuery plug-in is based
on javascript and so will will fire in client side. Now if up to you which on you should use.

jQuery Plug in is tested on IE, Firefox, Safari, Chrome and most other popular browser.


Hope this will help all to understand JQuery Validation in your asp.net web page.

If you run the sample project attached below and run it you will see JQuery validation result like this

You can download the sample code used in above example.



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses
Author: cristian         Company URL: http://ideasparaemprendedores.blogspot.com
Posted Date: 24/08/2009

Hi I have a problem that i can´t find the solution.

i have 1 txtbox, 1 dropdownlist and 1 select box. The dropdownlist load the select box. below some code to show how i'm validatiing the form

*************************
Script: (validate declaration)
*************************
rules:
{
cname:
{
required:true
},
ddlCities:
{
required: function (element){
return $("#ddlCities").val() < 1;
}
}
},

messages:
{
cname:
{
required: "<img src='img/cancel.png' with='20' height='20' title='Ingrese el nombre de su empresa'>"
},
ddlCities:
{
required: "<img src='img/cancel.png' with='20' height='20' title='Elija un Rubro'>"
}
}
******
HTML:

******
<form name="submitform" id="submitform" method="post" action="test.aspx" runat="server">

<input id="cname" name="cname">

<asp:DropDownList ID="ddlStates" runat="server">
<asp:ListItem Value="none">Select</asp:ListItem>
<asp:ListItem Value="1">Santa Fè</asp:ListItem>
<asp:ListItem Value="2">Buenos Aires</asp:ListItem>
</asp:DropDownList>

<select ID="ddlCities" runat="server">
</select>

this code is ok. when the select (ddlCities) don't have an option selected the validate library show the message asking choose one value of the list.

The problem is when i use this code inside of MasterPage, more exactly inside of ContentPlaceHolder. Cry

The text box validtion is ok, but the combo don't work.

I modified the code Script like:-->

*************************
Script: (validate declaration)
*************************
rules:
{
cname:
{
required:true
},
<%=ddlCities.ClientID%>:
{
required: function (element){
return $("#ddlCities").val() < 1;
}
}
},

messages:
{
cname:
{
required: "<img src='img/cancel.png' with='20' height='20' title='Ingrese el nombre de su empresa'>"
},
<%=ddlCities.ClientID%>:
{
required: "<img src='img/cancel.png' with='20' height='20' title='Elija un Rubro'>"
}
}

theorically, it should work, but it is not true.....

can you help me with this issue?


thanks in advance

Cristian

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend