jQuery 验证插件 - 检查字段是否有效(但不显示验证消息)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12480610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validation plugin - check if field is valid (but not show the validation message)
提问by arahant
I am using jQuery validation plugin and have defined a form validation function as shown below. Based on some user action I am running a custom JS function and in that function I just want to check whether the email and phone fields are valid. ButI don't want to validate them i.e. I don't want to show the validation errors, I just need to check if their value is valid.
我正在使用 jQuery 验证插件并定义了一个表单验证功能,如下所示。基于一些用户操作,我正在运行一个自定义 JS 函数,在该函数中,我只想检查电子邮件和电话字段是否有效。但我不想验证它们,即我不想显示验证错误,我只需要检查它们的值是否有效。
Something like
就像是
$('#email').isvalid();
I have checked out the elementmethod of Validator but that validates the element rather than just checking if it's value is valid. So In other words I am looking for a way to run the rules programmatically. Any help is appreciated!
我已经检查了Validator的element方法,但它验证了元素,而不仅仅是检查它的值是否有效。所以换句话说,我正在寻找一种以编程方式运行规则的方法。任何帮助表示赞赏!
Form validator function below:
表单验证器功能如下:
var validator = $("#olForm").validate({
rules: {
"email": {
required: true,
email: true
},
"phone": {
required: true,
digits: true,
minlength: 9,
maxlength: 11
}
}
});
采纳答案by Rob Wilkins
In response to the original jsfiddle, please see this version:
回应原来的jsfiddle,请看这个版本:
This is modified in three ways: firstly, at the bottom, a simple boolean value is set to equal whether both fields are valid (it's then alerted). As each valid() call returns a boolean value, you can simply and easily check the validity of all form elements.
这以三种方式修改:首先,在底部,一个简单的布尔值被设置为等于两个字段是否有效(然后发出警报)。由于每个 valid() 调用都返回一个布尔值,因此您可以简单轻松地检查所有表单元素的有效性。
Secondly, the validate option is expanded with a custom showErrors function that has no internal code. This function overrides the default behaviour for the validation errors, so that while the internal validity of the fields can be checked and the validity of the fields is handled, no actual error text is displayed, thereby enabling all the validation rules but eliminating the display of the validation results.
其次,使用没有内部代码的自定义 showErrors 函数扩展了验证选项。此函数覆盖验证错误的默认行为,以便在可以检查字段的内部有效性并处理字段的有效性的同时,不显示实际错误文本,从而启用所有验证规则但消除了显示验证结果。
Lastly, though it isn't explicitly requested in the initial question, I added the optional "onsubmit:false" setting, which overrides the validate call's default behaviour of preventing form submission unless all validation rules are successfully checked. This can be removed if you want to prevent submission (I assume you would), but I think it's a useful feature to be aware of. I included it here in case you had a special case where you would want the form to submit even if the form's validation had failed.
最后,虽然在最初的问题中没有明确要求,但我添加了可选的“onsubmit:false”设置,它会覆盖验证调用的默认行为,即阻止表单提交,除非成功检查了所有验证规则。如果您想阻止提交(我假设您会),可以将其删除,但我认为这是一个需要注意的有用功能。如果您有特殊情况,即使表单验证失败,您也希望表单提交,我将其包含在此处。
It's definitely worth reviewing the options list for validate; there are many ways you can adjust the validator's behaviour to suit exactly what you want.
绝对值得查看验证选项列表;您可以通过多种方式调整验证器的行为以完全满足您的需求。
回答by Dan H
There is an undocumented method check()that does this specifically without actually styling the fields or showing any errors:
有一个未记录的方法check()专门执行此操作,而无需实际设置字段样式或显示任何错误:
var validator = $('form').validate();
if( validator.check('#email') ){
// this field is valid but no styling was applied
}
回答by rnevius
There is a handy method called .checkForm()
(undocumented as of September 2015), which makes this really easy. Unlike calling .valid()
, this won't show form errors when it's called:
有一个方便的方法称为.checkForm()
(截至 2015 年 9 月未记录),这使得这非常容易。与 call 不同.valid()
,这在调用时不会显示表单错误:
// Returns a boolean
$('form').validate().checkForm();
回答by Mike Robinson
Sounds like you're looking for the .valid() method. Note you still have to run validate() against the whole form to initialize the widget.
听起来您正在寻找 .valid() 方法。请注意,您仍然必须对整个表单运行 validate() 以初始化小部件。