Example: Displays a message above the form, indicating how many fields are invalid when the user tries to submit an invalid form.
- $("#myform").validate({
- invalidHandler: function(event, validator) {
- // 'this' refers to the form
- var errors = validator.numberOfInvalids();
- if (errors) {
- var message = errors == 1
- ? 'You missed 1 field. It has been highlighted'
- : 'You missed ' + errors + ' fields. They have been highlighted';
- $("div.error span").html(message);
- $("div.error").show();
- } else {
- $("div.error").hide();
- }
- }
- });
success
Example: Add a class "valid" to valid elements, styled via CSS.
- $("#myform").validate({
- success: "valid",
- submitHandler: function() { alert("Submitted!") }
- });
Example: Add a class "valid" to valid elements, styled via CSS, and add the text "Ok!".
- $("#myform").validate({
- success: function(label) {
- label.addClass("valid").text("Ok!")
- },
- submitHandler: function() { alert("Submitted!") }
- });
The callback gets passed two arguments:
ignore (default:
)
- ":hidden"
Example: Ignores all elements with the class "ignore" when validating.
- $("#myform").validate({
- ignore: ".ignore"
- });
highlight (default:
)
- Adds errorClass (see the option) to the element
Example: Adds the error class to both the invalid element and its label
- $("#myform").validate({
- highlight: function(element, errorClass, validClass) {
- $(element).addClass(errorClass).removeClass(validClass);
- $(element.form).find("label[for=" + element.id + "]")
- .addClass(errorClass);
- },
- unhighlight: function(element, errorClass, validClass) {
- $(element).removeClass(errorClass).addClass(validClass);
- $(element.form).find("label[for=" + element.id + "]")
- .removeClass(errorClass);
- }
- })
The callback gets passed three arguments:
Type: Element
The invalid DOM element, usually an
.
- input
Type: String
Current value of the
option.
- errorClass
Type: String
Current value of the
option.
- validClass
unhighlight (default:
)
- Removes the errorClass
Type: Function()
Called to revert changes made by option highlight, same arguments as highlight.
来源: http://www.cnblogs.com/0zcl/p/7341988.html