gpt4 book ai didi

jQuery 验证插件 - 仅在检查变量后提交

转载 作者:行者123 更新时间:2023-12-01 03:55:23 26 4
gpt4 key购买 nike

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

最佳答案

您可以使用.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();

关于jQuery 验证插件 - 仅在检查变量后提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3329600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com