2012-08-02 94 views

回答

0

你可以在jQuery验证插件中创建一个新的规则。检查此链接jquery-validate - addMethod - how to apply custom rule referencing two text boxes?

在此规则中,您可以检查表单字段并相应地返回规则的值。

或者您也可以更改字段的规则。假设你有一个字段“的firstName”,这样你就可以像这样

firstName: 
{ 
    required: function(element) 
    { 
     // here you can add your custom code 
    } 
} 
+0

非常感谢响应。我会尝试。 – user519846 2012-08-02 09:54:36

0

添加自定义代码,您可以使用addMethod规则这个定制验证。 例如,如果您有一个窗体id =“mainForm”,并在下拉列表中显示状态列表。假设你的下拉是有ID =“drpState”的话,这样写

HTML 
------- 
<select> 
    <option value="-1">- Select -</option> 
    <option value="1">Aus</option> 
    <option value="2">Mogs</option> 
    <option value="3">Swut</option> 
</select> 

Jquery 
------- 
jQuery(document).ready(function() { 
$.validator.addMethod("selectState", function (value, element) { 
     return this.optional(element) || (value.indexOf('-1')); 
    }, "Please select state"); 

    $("#mainForm").validate({ 
     rules: {drpState:{selectState:true}, 
    }); 
}); 

请参见更多:http://ssthil.blogspot.in/2012/06/jquery-validate-multiple-fields-with.html 让我知道你的反馈

相关问题