简体   繁体   English

Jquery Validate插件中的几个自定义验证规则

[英]Several custom validate rules in Jquery Validate plugin

I am using Jquery Validate plugin to check the user input 我正在使用Jquery Validate插件检查用户输入

however, i found that the option is not sufficient and i decided to use custom rules 但是,我发现该选项不够用,因此我决定使用自定义规则

Are there any website provide those rules? 有没有网站提供这些规则?

What i would like to check is the format of date and integer only 我想检查的是 日期整数格式

Thank you 谢谢

you can customized jQuery validate plugin like this. 您可以像这样自定义jQuery验证插件。

jQuery("form#my-form").validate({
            rules: {
                //input field name "inputName"
                //implement your rules here
                inputName: {
                    minlength: 3
                }
            },
            messages: {
                inputName: {
                    required : 'Your customized message'
                }
            }
        });

you can add custom rules by extending the jquery validator 您可以通过扩展jQuery验证器来添加自定义规则

   jQuery.validator.addMethod("customdate", function(value, element) { 
   return this.optional(element) || {your rules}
     }, "eg YYYY-MM-DD");

see in here 在这里看到

http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage

For number only, you don't need to write your own rules.It already include the feature,try digits and number 仅用于数字,您无需编写自己的规则。它已经包括功能,尝试数字和数字

  1  $("#form").validate({
        rules:{
            inputtype:{required:true,digits:true}

        },
        messages:{
            inputtype:{required:"Please provide Number",digits,"Number Only!"}

        }
    });

    2.  $("#form").validate({
        rules:{
            inputtype:{required:true,number:true}

        },
        messages:{
            inputtype:{required:"Please provide Number",number,"Number Only!"}

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM