开发者

jQuery Validation Plugin - Submit only after checking a variable

I have done some small validation myself and end up 开发者_运维问答with either - myvalid = true or mayvalid = false. How can add this check to the validation I am doing already on my form using the Validation Plugin?


You can use .addMethod() to add custom validation methods to the Validation plugin.

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


Update:

Here's an example you can test: http://jsfiddle.net/W8EsU/

HTML

<form id='theForm'>
    <input id='test_field' name='test_field' value='jQuery' />
    <br>
    <input id='test_field2' name='test_field2' value='prototypejs' />
</form>​

jQuery

   // Add a validation method to the validator plugin
   //    that can be applied as a rule to whatever fields
   //    you want. That way you get your custom validation
   //    integrated into the functionality of the plugin
$.validator.addMethod(
    "mustIncludejQuery", 
    function(value, element) { 
        return value.toLowerCase().indexOf('jquery') > -1;
    }, 
    "You must type jQuery to be valid."
);

    // Apply the custom validation to the fields
$('#theForm').validate({
   rules: {
       test_field:'mustIncludejQuery',
       test_field2:'mustIncludejQuery'
   }
});

    // Demonstrates that they will be executed
    //    like any other validation rule.
$('#theForm').valid();
​
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜