当前位置: 动力学知识库 > 问答 > 编程问答 >

forms - How do add method in jquery.validate.js of bassisstance?

问题描述:

I want to validate my form as not to submit it if it equals a string.I have seen the documentation and tried adding it in my validation code.But doesn't work.I need to know where do i need to put the code(in my file or the file that i downloaded) and if possible the code itself to fulfil my requirement.

网友答案:

Create the method like this:

jQuery.validator.addMethod("name-of-validation-type", function(value, element) {
    var test = null; //Perform your test here        
    return this.optional(element) || test;
}, 'This is your invalid message.');

And then add the class name-of-validation-type to the input you want to validate.

You can also pass in additional an array of parameters to the method like this:

jQuery.validator.addMethod("name-of-validation-type", function(value, element, params) { .....

The parameters are passed in like this:

$('form').validate({      
    rules : {
        input_name : {
            name-of-validation-type: function(){ return param; }
        }
    }
}

Make sure you don't attach the validator to the form until your document is ready. So wrap your code in jQuery(function($) { //code to attach validator });

网友答案:

There is an option to add custom method for the plugin. Like:


jQuery.validator.addMethod("domain", function(value, element) { 
  return this.optional(element) || /^http:\/\/mycorporatedomain.com/.test(value); 
}, "Please specify the correct domain for your documents");

addMethod( name, method, [message] )

Edited Demo Example Link

分享给朋友:
您可能感兴趣的文章:
随机阅读: