1 login.jsp
<% @taglib uri= "/struts-tags" prefix= "s"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 4.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script src= "./demo/jquery-1.4.4.min.js" type= "Text/javascript" ></script>
<script src= "./demo/formvalidator-4.1.1.js" type= "Text/javascript" charset= "UTF-8" ></script>
<script src= "./demo/formvalidatorregex.js" type= "Text/javascript" charset= "UTF-8" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$.formvalidator.initconfig ({
FormID: "Form1",
Theme: "Arrowsolidbox",
Submitonce:true,
Onerror:function (msg, obj, errorlist) {
$ ("#errorlist"). empty ();
$.map (errorlist, Function (msg) {
$ ("#errorlist"). Append ("<li>" + msg + "</li>")
});
Alert (msg);
},
Ajaxprompt: ' There is data being verified asynchronously, please wait a moment ... '
});
$ ("#us"). Formvalidator ({
Onshowtext: "Please enter user name",
OnShow: "Please enter the user name, only input \" 111111\ "is Right",
onfocus: "User name at least 5 characters, maximum 10 characters",
Oncorrect: "This user name can be registered"
}). inputvalidator ({
Min:5,
Max:10,
OnError: "The user name you entered is illegal, please confirm"
}). ajaxvalidator ({
DataType: "JSON",
Async:true,
URL: "/jqueryformvalidatedemo/user/login.action",
Data: {
"Username": function () {
Return $ ("#us"). Val ();
}
},
Success:function (data) {
if (data = = "Yes")
return true;
return data;
},
Buttons: $ ("#button"),
Error:function (JQXHR, Textstatus, Errorthrown) {
Alert ("The server is not returning data, the server may be busy, please retry" + Errorthrown);
},
OnError: "The user name is not available, please replace the username",
Onwait: "The user name is being validated for legality, please wait ..."
});
$ ("#password1"). Formvalidator ({
OnShow: "Please enter your password",
onfocus: "At least 1 lengths",
Oncorrect: "Password valid"
}). inputvalidator ({
Min:1,
Empty: {
Leftempty:false,
Rightempty:false,
Emptyerror: "Cannot have empty symbols on either side of the password"
},
OnError: "The password cannot be empty, please confirm"
});
});
</script>
<body>
<form action= "" method= "Post" Name= "Form1" Id= "Form1" >
<input type= "Submit" name= "button" id= "button" value= "Submit"/> <br/>
<table border= "0px" style= "font-size:12px" width= "730px" >
<tr>
<TD align= "Right" width= "135px" > User name:</td>
<TD width= "265px" ><input type= "text" id= "US" name= "US"
Style= "width:120px" value= "Maodong"/>
</td>
<TD width= "330px" ><div id= "Ustip" style= "width:280px" ></div>
</td>
</tr>
<tr>
<TD align= "Right" > Password:</td>
<td><input type= "Password" id= "Password1" name= "Password1"
style= "width:120px"/>
</td>
<td><div id= "Password1tip" style= "width:280px" ></div>
</td>
</tr>
<tr>
<TD colspan= "3" ><div id= "Mstip" style= "width:280px" ></div>
</td>
</tr>
</table>
</form>
</body>
2 Struts Action
Package action;
Import java.io.IOException;
Import Java.util.HashMap;
Import Java.util.Map;
Import Org.apache.struts2.ServletActionContext;
Import Net.sf.json.JSONArray;
Import Com.opensymphony.xwork2.ActionSupport;
Import com.opensymphony.xwork2.Action;
public class Loginaction extends actionsupport{
Private String username;
Private String message;
Public String execute () {
if (Username.equals ("111111")) {
This.setmessage ("yes");
return action.success;
}
This.setmessage ("error");
return action.success;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetMessage () {
return message;
}
public void Setmessage (String message) {
this.message = message;
}
}
3. Struts. Xml
<?xml version= "1.0" encoding= "UTF-8"?
<! DOCTYPE struts public
-//apache software foundation//dtd struts Configuration 2.0//en
"Http://struts.apache.org/dtds/struts-2.0.dtd";
<struts>
<package name= "user" namespace= "/user" extends= "Json-default";
<action name= "Login" class= "action. Loginaction
<result type= "json" >
<param name= "root" >message</param>
</result>
</action>
</package>
</struts>
JQuery formvalidate Demo uses struts to verify the existence of the user name in the asynchronous way