I have written a Post article to query whether the username is duplicated using Ajax. This time we use the jquery valiins validate for implementation, which is simpler. Compared with XMLHttpRequest, we do not need to consider browser compatibility issues, this is also one of the features of jquery's Lightweight Framework.
ASP. net mvc rc and aspx are used here.CodeAs follows:
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind = " Testvalidation1.aspx. CS " Inherits = " Demomvcform. Views. formdemo. testvalidation1 " %>
<! 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 > Demo </ Title >
< Script SRC = "Http://www.cnblogs.com/Scripts/jquery-1.2.6.min.js" Type = "Text/JavaScript" > </ Script >
< Script SRC = "Http://www.cnblogs.com/Scripts/jquery.validate.js" Type = "Text/JavaScript" > </ Script >
< Script Type = "Text/JavaScript" >
$ (Document). Ready ( Function (){
$ ( " # Form-sign-up " ). Validate ({
Rules :{
Login :{
Required: True ,
// Here invoke related action
Remote: ' <% = URL. Action ("isloginavailable", "formdemo") %> '
}
},
Messages :{
Login :{
Required: " Enter User Name " ,
Remote: jquery. Format ( " {0} already used " )
}
},
// Set this class to error-labels to indicate valid Fields
Success: Function (Label ){
// Set as text for IE
Label.html ( " " ). Addclass ( " Checked " );
}
});
});
</ Script >
</ Head >
< Body >
< Form Action = "<% = URL. Action (" Register "," formdemo ") % > "Method =" Post "id =" form-sign-up ">
< H1 > Demo form </ H1 >
< Table ID = "Inputarea" >
< Tr >
< TD > User Name (try entering Petter ): </ TD >
< TD > < Input Type = "Text" Name = "Login" ID = "Login" /> </ TD >
</ Tr >
< Tr >
< TD Colspan = "2" Align = "Center" > < BR /> < Input Type = "Submit" /> </ TD >
</ Tr >
</ Table >
</ Form >
</ Body >
</ Html >
Asp.net MVC supports JSON, and all the results returned in JSON are directly used as jsonresult. Check the code CS, which is very simple:
CS
/// <Summary>
/// Formdemocontroller
/// </Summary>
/// <Remark> Author: petterliu DEV-LIUJUNYUAN </Remark>
Public Class Formdemocontroller: Controller
{
Public Jsonresult isloginavailable ( String Login)
{
// Todo: Do the validation
Jsonresult result = New Jsonresult ();
If (Login = " Petter " )
Result. Data = False ;
Else
Result. Data = True ;< P>
return result;
}< BR >}
references:
Asp.net MVC http://www.asp.net/mvc
jquery http://www.jquery.com
jquery-validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/
author: petterliu http://www.cnblogs.com/wintersun/