jQuery Validate Plugin - 触发单个字段的验证

发布于 2024-08-30 09:25:31 字数 207 浏览 5 评论 0原文

我有一个可以选择通过 facebook connect 预先填充的表格。用户连接后,他们的姓名和电子邮件会自动填写。问题是,这不会触发远程验证来检查电子邮件是否已存在。

有没有办法可以单独调用该字段的验证?像这样的东西:

$('#email-field-only').validate()

是个好主意。搜索了文档但没有运气。

I've got a form that can optionally be pre-populated via facebook connect. Once a user connects, their name and email are automatically filled in. The problem is that this doesn't trigger the remote validation to check if the email already exists.

Is there a way I could call the validation on that field alone? Something like:

$('#email-field-only').validate()

would be idea. Searched through the docs with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

迷你仙 2024-09-06 09:25:31

此方法似乎可以满足您的要求:

$('#email-field-only').valid();

编辑

API 已更改,请参阅 Paul 的回答

This method seems to do what you want:

$('#email-field-only').valid();

Edit

API has changed, see Paul's answer.

dawn曙光 2024-09-06 09:25:31

使用 Validator.element()

验证单个元素,有效则返回 true,否则返回 false
否则。

下面是 API 中显示的示例:

var validator = $( "#myform" ).validate();
validator.element( "#myselect" );

.valid() 验证整个表单,正如其他人指出的那样出去。 API 说:

检查所选表单是否有效或者是否全部选中
元素有效。

Use Validator.element():

Validates a single element, returns true if it is valid, false
otherwise.

Here is the example shown in the API:

var validator = $( "#myform" ).validate();
validator.element( "#myselect" );

.valid() validates the entire form, as others have pointed out. The API says:

Checks whether the selected form is valid or whether all selected
elements are valid.

冷夜 2024-09-06 09:25:31
$("#FormId").validate().element('#FieldId');
$("#FormId").validate().element('#FieldId');
ゞ记忆︶ㄣ 2024-09-06 09:25:31

由于某种原因,一些其他方法在字段被聚焦/模糊/更改或尝试提交之前不起作用......这对我有用。

$("#formid").data('validator').element('#element').valid();

必须深入研究 jquery.validate 脚本才能找到它......

For some reason, some of the other methods don't work until the field has been focused/blured/changed, or a submit has been attempted... this works for me.

$("#formid").data('validator').element('#element').valid();

Had to dig through the jquery.validate script to find it...

勿忘心安 2024-09-06 09:25:31

如果您想验证单个表单字段,但不希望触发 UI 并显示任何验证错误,您可以考虑使用 Validator.check() 方法,该方法将返回给定字段是否通过验证。

这是例子

var validator = $("#form").data('validator');
if(validator.check('#element')){
    /*field is valid*/
}else{
    /*field is not valid (but no errors will be displayed)*/
}

If you want to validate individual form field, but don't want for UI to be triggered and display any validation errors, you may consider to use Validator.check() method which returns if given field passes validation or not.

Here is example

var validator = $("#form").data('validator');
if(validator.check('#element')){
    /*field is valid*/
}else{
    /*field is not valid (but no errors will be displayed)*/
}
南渊 2024-09-06 09:25:31

当您设置验证时,您应该保存验证器对象。您可以使用它来验证各个字段。

<script type="text/javascript">
var _validator;
$(function () {    
     _validator = $("#form").validate();   
});

function doSomething() {    
     _validator.element($('#someElement'));
}
</script> 

-- 与 这个类似的问题

When you set up your validation, you should be saving the validator object. you can use this to validate individual fields.

<script type="text/javascript">
var _validator;
$(function () {    
     _validator = $("#form").validate();   
});

function doSomething() {    
     _validator.element($('#someElement'));
}
</script> 

-- cross posted with this similar question

方觉久 2024-09-06 09:25:31

如果您想对表单上的“某些元素”(不是所有元素)进行验证。您可以使用此方法:

$('input[name="element-one"], input[name="element-two"], input[name="element-three"]').valid();

希望它对大家有帮助:)

已编辑

in case u wanna do the validation for "some elements" (not all element) on your form.You can use this method:

$('input[name="element-one"], input[name="element-two"], input[name="element-three"]').valid();

Hope it help everybody :)

EDITED

虫児飞 2024-09-06 09:25:31

我遇到了几乎相同的问题,但对于我的情况,我只需要验证 4 个字段。
这就是我所做的;我向我想要验证的元素添加了一个类。然后我在该特定类上调用了 validate() 。

$(".elementClass").valid();

I had almost the same issue but for my case, I needed to validate just 4 fields.
This is what I did; I added a class to the elements I wanted to validate. Then I called the validate() on that specific class.

$(".elementClass").valid();
中二柚 2024-09-06 09:25:31
$("#element").validate().valid()
$("#element").validate().valid()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文