jQuery技术

Jquery.validate.js表单验证详解(3)

字号+ 作者:H5之家 来源:H5之家 2017-08-31 13:00 我要评论( )

对了,还有一个坑,百度/谷哥jquery.validate.js,会出现博客园的这篇文章 jQuery验证控件jquery.validate.js使用说明+中文API ,当时我想 自定义错误信息 ,网上很多方法有用如下代码的方法,但我也引入了jquery.m

对了,还有一个坑,百度/谷哥jquery.validate.js,会出现博客园的这篇文章 jQuery验证控件jquery.validate.js使用说明+中文API ,当时我想 自定义错误信息 ,网上很多方法有用如下代码的方法,但我也引入了jquery.metadata.js,然而并没有反应。fuck! 

使用class="{}"的方式,必须引入包:jquery.metadata.js 可以使用如下的方法,修改提示内容: class="{required:true,minlength:5,messages:{required:'请输入内容'}}"

解决方法:这个问题的实质是,我要对select标签进行验证,比如该标签是必填的,那我肯定要给标签加上required=“true",但是输入内容长度要如何加在标签?用上面代码的方法是不行的!! 还有,如果你自定义了验证方法,比如前端我自定义验证IP,那我如何让标签验证时调用这写的验证方法。其实很简单,没啥技术含量。看代码,代码现在没有,明天上班再看看

-----------------------分割线-----------------------

四、jquery.validate.js记录

submitHandler  (default:  native for m submit )

Type:  ()

Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to  after it is validated.

Example: Submits the form via Ajax when valid.

$("#myform").validate({ submitHandler: function(form) { $(form).ajaxSubmit(); } });

Example: Use submitHandler to process something and then using the default submit. Note that "form" refers to a DOM element, this way the validation isn't triggered again.

$("#myform").validate({ submitHandler: function(form) { // do other things for a valid form form.submit(); } });

invalidHandler

Type:  ()

Callback for custom code when an invalid form is submitted. Called with an event object as the first argument, and the validator as the second.

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

Type:  or  ()

If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label (as a jQuery object) and the validated input (as a DOM element). The label can be used to add a text like "ok!".

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:

  • label

    Type: 

    The error label. Use to add a class or replace the text content.

  • element

    Type: 

    The element currently being validated, as a DOMElement.

  • ignore (default:  ":hidden" )

    Type: 

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • jquery.validate.js简介

      jquery.validate.js简介

      2017-02-25 13:04

    • JQuery表单验证插件jQuery.validate.js

      JQuery表单验证插件jQuery.validate.js

      2016-11-13 14:00

    • jQuery Validate.js参数以及使用教程

      jQuery Validate.js参数以及使用教程

      2015-09-23 15:15

    网友点评