jquery.validate + Custom

jquery.validate remote and custom authentication methods



$ (Function () {

var validator = $ ("# enterRegForm"). validate ({
debug: false, / / debug mode to cancel the default submit function submit
/ / ErrorClass: "error", / / default style class for the error: error
/ / ValidClass: "check", / / verify the success of the style, the default string is valid
focusInvalid: true, / / form is submitted, the focus point to the first one did not pass the verification domain
/ / FocusCleanup: true; / / focal point to the wrong field, hide the error message can not be used in conjunction with the focusInvalid!
onkeyup: true,
errorElement: "div",
submitHandler: function (form) {/ / handle form submission, is a callback function with a parameter: form
form.submit (); / / submit the form
}

rules: {
"Enterprise.enName": {
required: true,
minlength: 6,
remote: {
url: "/ nameServlet", / / background processing
type: "get", / / data transmission mode
dataType: "json", / / accept the data format
data: {/ / data to pass
enName: function () {
return $ ("# enName"). val ();
}
}

}
}

"User.passWord": {
required: true,
rangelength: [6,18]
}
passWordConf: {
required: true,
rangelength: [6,18],
equalTo: "# passWord" / / here is <input> passWord thought it was the name of a start value of it
}

}

messages: {/ / custom validation messages
"Enterprise.enName": {
required: "Please fill in the company name!"
minlength: $. format ("at least {0} characters!")
remote: $. format ("the company name already exists!")

}
"User.passWord": {
required: "Please confirm your password!"
rangelength: $. format ("password to {0} - {1} characters!")
}
passWordConf: {
required: "Please confirm your password!"
rangelength: $. format ("Confirm Password to {0} - {1} characters!")
equalTo: "Confirm the password and the password should be the same!"
}

errorPlacement: function (error, element) {/ / verify the messages are placed where
/ / Error.appendTo (element.parent ("td"). Next ("td"). Children (". Msg"));
error.appendTo (element.parent (". field"). next ("div"));
}
highlight: function (element, errorClass) {/ / set the highlight for the validation of the form
$ (Element). AddClass (errorClass);
}
success: function (div) {
div.addClass ("valid");
}
});

});

Custom methods;

Create a js file: $ (document). Ready (function () {
/ / Character minimum length of authentication (a Chinese character length of 2)
jQuery.validator.addMethod ("stringMinLength", 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);
}, $. Validator.format ("length of not less than {0 }!"));

/ / Verify the maximum length of characters (a Chinese character length of 2)
jQuery.validator.addMethod ("stringMaxLength", 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);
}, $. Validator.format ("length can not exceed {0 }!"));

/ / Character verification
jQuery.validator.addMethod ("stringCheck", function (value, element) {
return this.optional (element) | | / ^ [\ u0391-\ uFFE5 \ w ]+$/. test (value);
}, "Only include Chinese characters, English letters, numbers and underscores");

/ / Two bytes in the text
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]);
}, "Please be sure to enter a value in between 3-15 bytes (a word count in 2 bytes)");

/ / Character verification
jQuery.validator.addMethod ("string", function (value, element) {
return this.optional (element) | | / ^ [\ u0391-\ uFFE5 \ w ]+$/. test (value);
}, "May not contain special characters!");
/ / Beginning of the string must verify the specific
jQuery.validator.addMethod ("begin", function (value, element, param) {
var begin = new RegExp ("^" + param);
return this.optional (element) | | (begin.test (value));
}, $. Validator.format ("{0} must start !"));
/ / Validate the input value is not the same twice
jQuery.validator.addMethod ("notEqualTo", function (value, element, param) {
return value! = $ (param). val ();
}, $. Validator.format ("twice for the same !")); not
/ / Verify the values are not allowed with a specific value is equal to
jQuery.validator.addMethod ("notEqual", function (value, element, param) {
return value! = param;
}, $. Validator.format ("input values are not allowed to {0 }!"));

/ / Verify the value must be greater than a specific value (not equal)
jQuery.validator.addMethod ("gt", function (value, element, param) {
return value> param;
}, $. Validator.format ("Input value must be greater than {0 }!"));

/ / Verify the value of not more than two decimal places
jQuery.validator.addMethod ("decimal", function (value, element) {
var decimal = / ^ -? \ d + (\. \ d {1,2 })?$/;
return this.optional (element) | | (decimal.test (value));
}, $. Validator.format ("not more than two decimal places !"));
/ / Alphanumeric
jQuery.validator.addMethod ("alnum", function (value, element) {
return this.optional (element) | | / ^ [a-zA-Z0-9 ]+$/. test (value);
}, "Can only include letters and numbers");
/ / Characters
jQuery.validator.addMethod ("chcharacter", function (value, element) {
var tel = / ^ [\ u4e00-\ u9fa5 ]+$/;
return this.optional (element) | | (tel.test (value));
}, "Please enter the characters");
/ / ID number verification
jQuery.validator.addMethod ("isIdCardNo", function (value, element) {
return this.optional (element) | | / ^ [1-9] \ d {7} ((0 \ d) | (1 [0-2 ]))(([ 0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $ /. test (value )||/^[ 1-9] \ d {5} [1-9] \ d {3} ((0 \ d) | (1 [0-2 ]))(([ 0 | 1 | 2] \ d) | 3 [0-1]) ((\ d {4}) | \ d {3} [AZ ])$/. test (value);
}, "Please enter your ID number correctly");

/ / Phone number verification
jQuery.validator.addMethod ("isMobile", function (value, element) {
var length = value.length;
var mobile = /^((( 13 [0-9] {1}) | (15 [0-9] {1})) + \ d {8 })$/;
return this.optional (element) | | (length == 11 & & mobile.test (value));
}, "Please enter your phone number correct");

/ / Phone number verification
jQuery.validator.addMethod ("isTel", function (value, element) {
var tel = / ^ \ d {3,4} -? \ d {7,9} $ /; / / Phone number format 010-12345678
return this.optional (element) | | (tel.test (value));
}, "Please enter your phone number correct");

/ / Tel (mobile phone / phone can be) to verify
jQuery.validator.addMethod ("isPhone", function (value, element) {
var length = value.length;
var mobile = /^((( 13 [0-9] {1}) | (15 [0-9] {1})) + \ d {8 })$/;
var tel = / ^ \ d {3,4} -? \ d {7,9} $ /;
return this.optional (element) | | (tel.test (value) | | mobile.test (value));

}, "Please fill in your contact phone number correctly");

/ / Postal Code authentication
jQuery.validator.addMethod ("isZipCode", function (value, element) {
var tel = / ^ [0-9] {6} $ /;
return this.optional (element) | | (tel.test (value));
}, "Please enter your zip code correctly");

});

The document must first form is referenced again to verify.

Note: remote in the url for the servlet, but how to write it this servlet

The beginning thought it was the same as with the usual: out.println ("User name" + name + "already exists!");

Although you can verify, but the problem still exists: Enter a name that does not exist, or suggest the presence of.

Later, or Google, to see someone else is guilty of such an error, the solution returns true, false.

But is the void doGet type, return true is not feasible.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of jquery.validate + Custom

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries