SIGN UP MEMBER LOGIN:    
ARTICLE

Validation using JQuery with ASP.Net

Posted by Purushottam Rathore Articles | JQuery January 18, 2011
In this article I would like to share with you how to use validation using JQuery in asp.net.
Reader Level:
 

In this article I would like to share with you how to use validation using JQuery in asp.net. JQuery plays a major role for any technology. It is simple to understand and easy to use. It can create impressive animations and interactions.

Now I am discussing about this article using the simple article.

First of all download uploaded file. In this application I am attaching three JQueries as follows:

jquery-1.3.2.js
jquery-latest.js
jquery.validate.js

Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Validation Example using JQuery</title>

    <link rel="stylesheet" href="ValidationStyle.css" type="text/css" />

 

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

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

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

 

<%--this javascriptis used to show the message after valid statement.--%>

    <script type="text/javascript">

        jQuery.validator.setDefaults({

            debug: true,

            success: "valid"

        }); ;

    </script>

<%--write the following javascript for passing the parameter for jquery--%>

    <script type="text/javascript">

        $(document).ready(function() {
            $("#form1").validate({
                rules: {
                    <%=txtUserName.UniqueID %>: {
                        minlength: 5,
                        required: true
                    },
                    <%=txtPassword.UniqueID %>: {
                        minlength: 5,
                        required: true
                    },
                    <%=txtEmail.UniqueID %>: {
                        required: true
                    },                   
                    <%=txtURL.UniqueID %>: {
                         required: true
                    }
                }, messages: {
                    <%=txtUserName.UniqueID %>:{
                        required: "Plaese enter your name",
                        minlength: "User name must be atleaet of 5 characters"
                        },
                    <%=txtPassword.UniqueID %>:{
                        required: "Plaese enter your password",
                        minlength: "Password must be atleaet of 5 characters"
                        },
                    <%=txtEmail.UniqueID %>:{
                        required: "Plaese enter your Email Id",
                        },
                    <%=txtURL.UniqueID %>:{
                        required: "Plaese enter Website URL",
                        }                 
                  }
            });
        });
    </script>

 

</head>

<body>

    <form id="form1" runat="server">

    <table width="50%" cellpadding="2" cellspacing="4" style="border: solid 1px navy;

        background-color: #d5d5d5;">

        <tr>

            <td colspan="2" align="center">

                <b>Validation Example using JQuery</b>

            </td>

        </tr>

        <tr>

            <td align="right" width="75px">

                User Name :

            </td>

            <td>

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

            </td>

        </tr>

        <tr>

            <td align="right">

                Password :

            </td>

            <td>

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

            </td>

        </tr>

        <tr>

            <td align="right">

                Email Id :

            </td>

            <td>

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

            </td>

        </tr>

        <tr>

            <td align="right">

                URL :

            </td>

            <td>

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

            </td>

        </tr>

        <tr>

            <td align="right">

                &nbsp;

            </td>

            <td align="left">

                <asp:Button ID="btnSubmit" runat="server" Text="Submit" />

            </td>

        </tr>

    </table>

    </form>

</body>

</html>


Use the following Style sheet


StyleSheet:

body{}

#field{margin-left: .5em;float: right;}

#field, label{float: left;font-family: Arial, Helvetica, sans-serif;font-size: small;}

br{clear: both;}

input{border: 1px solid black;margin-bottom: .5em;}

input.error{border: 1px solid red;}

label.error{position:absolute;background: url('Images/unchecked.gif') no-repeat;padding-left: 20px; margin-left: .3em; vertical-align: middle;background-color: #FFEBE8;border: solid 1px red;width: 250px;}


label.valid
{position:inherit;background: url('Images/checked.gif') no-repeat;display: block;width: 16px;height: 16px;border: none;vertical-align:top;}


Output:


image1.JPG
Figure 1: All fields are required

image2.JPG
Figure 2: In the first text box range should be atleast 5 characters.

image3.JPG
Figure 3: The input of the first textbox is valid. Here blue icon represent the valid data.

image4.JPG
Figure 4: Password range must be atleast 5 characters

image5.JPG
Figure 5: Check email validation

image6.JPG
Figure 6: Valid email.

image7.JPG
Figure 7: Check valid url

image8.JPG 
Figure 8: All inputs are valid.

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Can u give me javascript validation when we use TextBox validation then error message display in below of the TextBox

Posted by Manoranjan Behera Apr 29, 2011

that jQury part very well

Posted by Manoranjan Behera Apr 29, 2011

Hello, great artical. I would like to know how you would check multiple textboxes for correct emails entered. The page is for sending emails, the user can send a email to multiple addresses. The problem is that there is only one textbox, the rest are created dynamically when the user clicks on a link "Add Recipient". So it is uncertian how many textboxes there will be. Each textbox created has an ID of: "txtBox" plus a integer number that is one more than the last. How would i validate these textboxes with jQuery?

Posted by christo kleynhans Feb 16, 2011

I am getting error on visual studio 2008 after inserting the code

Posted by kenny kenny Feb 05, 2011

Good article. keep it up.

Posted by Raj Kumar Jan 18, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor