jQuery验证触发器错误消息 [英] jQuery validation trigger error message

查看:77
本文介绍了jQuery验证触发器错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.我想在元素上触发jquery-valdation错误消息,即使它是有效的.

I have a kinda wierd problem. I want to trigger a jquery-valdation errormessage on an element even thou it's valid.

场景: 我有一个大表格.输入之一是用于PersonalId.输入该PersonalId是可选的. 除了输入,我还有一个普通按钮(不是提交按钮).如果单击该按钮(从ajax获取信息),而PersonalId字段为空,则我想触发该错误消息.

Scenario: I have a large form. One of the inputs is for PersonalId. It's optional to enter that PersonalId. Beside that input i have a normal button (not the submit-button). If you click on that (fetching information from ajax) and the PersonalId field is empty i want to trigger that Error-message.

使用jQuery Validate是否可行?还是需要创建自己的函数?

Is that possible with jQuery Validate or do i need to create my own function?

推荐答案

使用jQuery Validate是否可行?还是需要创建自己的函数?

Is that possible with jQuery Validate or do i need to create my own function?

两者都是.您可以使用showErrors函数在表单上手动显示错误,但是由于您不希望在提交表单时启用此规则,因此您必须执行您自己检查输入中的值.

Both, sort of. You can use the showErrors function to display errors on the form manually, but since you don't want this to be a rule that is enabled when you submit the form, you'll have to do your own checking for the value in the input.

类似的东西:

var $validator = $("#myform").validate();
$("#checkid").click(function() {
    var errors;

    if (!$("#personalid").val()) {
        /* Build up errors object, name of input and error message: */
        errors = { personalid: "Please enter an ID to check" };
        /* Show errors on the form */
        $validator.showErrors(errors);            
    }        
});

示例: http://jsfiddle.net/andrewwhitaker/XjZer/

请注意,如果personalid字段中没有任何内容,表单仍将提交.

Note that the form will still submit if there is nothing in the personalid field.

这篇关于jQuery验证触发器错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆