Recently in doing a user registration login page, the data search process found a very good client authentication for the Acura-jquery.validate.
It is a plugin of the famous JavaScript package jquery, in fact it has some other plug-ins should be cool, waiting to learn slowly
Official address: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
jquery User's Manual: Http://jquery.org.cn/visual/cn/index.xml
Development is very simple and straightforward to use.
My Code:
Java code
- $ (document). Ready (function () {
- /* Set default Properties */
- $.validator.setdefaults ({
- Submithandler:function (form) {Form.submit ();}
- });
- Chinese characters two bytes
- JQuery.validator.addMethod ("Byterangelength", function (value, element, param) {
- var length = Value.length;
- For (var i = 0; i < value.length; i++) {
- if (value.charcodeat (i) > 127) {
- length++;
- }
- }
- return this.optional (element) | | (length >= param[0] && length <= param[1]);
- }, "Make sure the value entered is between 3-15 bytes (2 bytes in one text)");
- /* Append a custom validation method */
- Identification Number Verification
- JQuery.validator.addMethod ("Isidcardno", function (value, Element) {
- return this.optional (element) | | Isidcardno (value);
- }, "Please enter your ID number correctly");
- Character verification
- JQuery.validator.addMethod ("UserName", function (value, Element) {
- return this.optional (element) | |/^[\u0391-\uffe5\w]+$/.test (value);
- }, "User name can only include Chinese characters, English letters, numbers and underscores";
- Mobile phone number Verification
- JQuery.validator.addMethod ("IsMobile", function (value, Element) {
- var length = Value.length;
- return this.optional (element) | | (length = = &&/^ (((13[0-9]{1}) | ( 15[0-9]{1})) +\d{8}) $/.test (value));
- }, "Please fill in your mobile phone number correctly");
- Phone number Verification
- JQuery.validator.addMethod ("Isphone", function (value, Element) {
- var tel =/^ (\d{3,4}-?)? \d{7,9}$/g;
- return this.optional (element) | | (Tel.test (value));
- }, "Please fill in your phone number correctly");
- ZIP Code Verification
- JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
- var tel =/^[0-9]{6}$/;
- return this.optional (element) | | (Tel.test (value));
- }, "Please fill in your zip code correctly");
- $ (regfrom). Validate ({
- /* Set validation rules */
- Rules: {
- UserName: {
- Required: true,
- UserName: True,
- Byterangelength: [3,]
- },
- Password: {
- Required: true,
- MinLength: 5
- },
- Repassword: {
- Required: true,
- MinLength: 5,
- Equalto: "#password"
- },
- Question: {
- Required: true
- },
- Answer: {
- Required: true
- },
- Realname: {
- Required: true
- },
- Cardnumber: {
- Isidcardno: True
- },
- Mobilephone: {
- IsMobile: True
- },
- Phone: {
- Isphone: True
- },
- Email: {
- Required: true,
- Email: true
- },
- ZipCode: {
- Iszipcode:True
- }
- },
- /* SET error message */
- Messages: {
- UserName: {
- Required: "Please fill in the user name",
- Byterangelength: "User name must be between 3-15 characters (2 characters in one text)"
- },
- Password: {
- Required: "Please fill in the password",
- Minlength:jQuery.format ("input {0}.")
- },
- Repassword: {
- Required: "Please fill in the confirmation password",
- Equalto: "two times password input is not the same"
- },
- Question: {
- Required: "Please fill in your password prompt question"
- },
- Answer: {
- Required: "Please fill in your password prompt answer"
- },
- Realname: {
- Required: "Please fill in your real name"
- },
- Email: {
- Required: "Please enter an email address",
- Email: "Please enter a valid email address"
- }
- },
- /* Where error message is displayed */
- Errorplacement:function (Error, Element) {
- Error.appendto (Element.parent ());
- },
- /* Processing when validation passes */
- Success:function (label) {
- //Set as text for IE
- Label.html (""). AddClass ("checked");
- },
- /* Do not verify when getting focus */
- Focusinvalid: false,
- OnKeyUp: false
- });
- When the input box gets focus, the style setting
- $ (' input '). focus (function () {
- if ($ (this). Was (": Text") | | $ (This). Is (":p assword"))
- $ (this). addclass (' focus ');
- if ($ (this). Hasclass (' Have_tooltip ')) {
- $ (this). Parent (). Parent ().removeclass ('field_normal '). addclass (' field_focus ');
- }
- });
- Style settings when the input box loses focus
- $ (' input '). blur (function () {
- $ (this). Removeclass (' focus ');
- if ($ (this). Hasclass (' Have_tooltip ')) {
- $ (this). Parent (). Parent ().removeclass ('field_focus '). addclass (' field_normal ');
- }
- });
- });
Online information Some people say that it has a conflict with the prototype package, I have not used at the same time, this is not very clear, but I have found a problem:
For the minimum/large length of the verification method, the author may consider that everyone's naming habits are different, at the same time do the minlength and MinLength (MaxLength and MaxLength) methods, which should be possible, but for the user message, can only be defined for minlength (maxlength) to invoke a user-defined message,
Otherwise, it simply calls the package's default message, but the exact reason is not yet known. At the same time, this plugin provides localized messages, but for beginners here, how to use it still needs to be explored!
The need for client-side validation--jquery.validator