Several custom validate rules in Jquery Validate plugin

断了今生、忘了曾经 提交于 2019-12-12 06:27:57

问题


I am using Jquery Validate plugin to check the user input

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


回答1:


you can customized jQuery validate plugin like this.

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



回答2:


you can add custom rules by extending the jquery validator

   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

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!"}

        }
    });


来源:https://stackoverflow.com/questions/9970434/several-custom-validate-rules-in-jquery-validate-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!