How To jQuery Validation With ASP.Net


In this example, we will explain the basics used jQuery validation in ASP.Net Web Form. Using a library to do form validations can save much of your development time. The jQuery Form validation library is the most popular validation library and I think one of the easiest libraries to use in web forms.

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

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"  type="text/javascript"></script>   
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js" type="text/javascript"></script>  
        <script type="text/javascript">                
            $(document).ready(function () {  
                $("#form1").validate({  
                    rules: {  
                        <%=txtName.UniqueID %>:{  
                            required:true  
                        },   
                    },  
                    messages: {  
                        <%=txtName.UniqueID %>:{  
                            required: "Name is required."  
                        },  
                    },  
                });  
            });         
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            Name
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        </form>
    </body>
    </html>


Post a Comment

0 Comments