【问题标题】:jQuery Validation Plugin - Submit only after checking a variablejQuery Validation Plugin - 仅在检查变量后提交
【发布时间】:2010-07-25 14:46:46
【问题描述】:

我自己做了一些小的验证,最终得到 - myvalid = true 或 mayvalid = false。如何使用验证插件将此检查添加到我已经在表单上执行的验证中?

【问题讨论】:

    标签: jquery validation variables submit


    【解决方案1】:

    您可以使用.addMethod() 将自定义验证方法添加到验证插件。

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


    更新:

    这是一个您可以测试的示例: 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();
    ​
    

    【讨论】:

    • +1 - 这确实是要走的路,将其添加为您要检查的任何元素的规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2014-08-20
    相关资源
    最近更新 更多