Using jquery's validate plug-in to do client-side validation is handy, and here's an example of using the Validate plug-in to authenticate user registration information.
This example uses the 1.5 version .
The example is done under SSH with the following code:
registe.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Main methods of background Registeaction.java
Public String Execute () throws Exception {Map session = Actioncontext.getcontext (). GetSession ();
String ver2 = (string) session.get ("Rand");
Session.put ("Rand", NULL); Determine if the validation code is correct if (vercode.equals (ver2)) {if (Usermanager.validname (username)) {if Usermanager.adduser (username, pas
Sword, email) > 0) return SUCCESS; else Addactionerror ("registration failed, please try again!")
"); } else {Addactionerror ("This username already exists, please re-enter!")
");
} else {addactionerror ("Verify code mismatch, please re-enter");
} return INPUT;
//Verify that the user name is available with public String validname () throws Exception {System.out.println (username);
Boolean flag = Usermanager.validname (username);
HttpServletResponse response = Servletactioncontext.getresponse ();
Response.setdateheader ("Expires", 0);
Response.AddHeader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setcontenttype ("Text/plain;charset=utf-8");
if (flag) Response.getwriter (). Write ("true");
else Response.getwriter (). Write ("false"); REsponse.getwriter (). Flush ();
Returns null because the content is output directly without passing through a JSP.
return null;
}
The effect chart is as follows:
Note: the method of using remote asynchronously to authenticate the username should be output by Response.getwriter (). Write ("true"), rather than returning the string as normal.
For more detailed information on plug-ins you can view the "jQuery Validate Verification Plug-in usage details."
In addition, jquery also supports the dynamic addition of checksums to controls, such as:
Copy Code code as follows:
("#email"). Rules ("Add", {required:true, email:true});
Note, however, that if you dynamically add checksums to the elements in a collection, you need to iterate over each element because jquery implicitly implements the collection operation, but the validate plugin does not. For example:
$ (". Quantity"). each (function () {
$ (this). Rules ("Add", {digits:true,required:true});
};
The above is the entire content of this article, I hope to help you learn.