jQuery远程验证-验证数据是否在字段中 [英] jquery validation remote - validate whether data in field or not

查看:77
本文介绍了jQuery远程验证-验证数据是否在字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述:
在我的表单中,我有一个需要远程验证的字段.在远程调用中,我发送form元素的值以及该元素可能依赖的其他几个form元素的值(用于服务器验证).

General Description:
In my form, I have a field that requires remote validation. In the remote call, I send the value of the form element as well as the values of a couple other form elements on which this element may depend (used on the server for validation).

调用远程验证时,效果很好.问题是仅当form元素中有值时才调用远程验证.根据发送给远程验证的参数,可能需要此表单元素,或者可能存在其他验证问题.

The remote validation, when called, works great. The problem is that the remote validation is only called when there is a value in the form element. Depending on the params sent to the remote validation, this form element may be required or there may be other validation issues.

无论该字段中是否包含数据,如何强制对该字段进行远程验证?

How can I force the remote validation on this field whether it has data in it or not?

特定方案:
得到了角色列表,并输入了销售员代码文本.
服务器端逻辑:(1)如果选择了销售人员角色,则需要销售人员代码; (2)如果未选择销售人员角色,则销售人员代码必须为空.

Specific Scenario:
Got a roles list, and a salesperson code text input.
Server side logic: (1) if salesperson role selected, salespersoncode required; (2) if salesperson role not selected, salespersoncode must be empty.

请注意,我确实还有其他要发送到服务器的表单元素值,为简洁起见,这里没有包括其他服务器端逻辑,因为它们不会给这个问题增加价值,只会使事情变得混乱.

Note that I do have other form element values I send to the server and there is other server side logic that I did not include here for brevity since they do not add value to this question and will only confuse things.

<select name="roles" id="roles" multiple="multiple" size="5"></select>
<input type="text" name="salespersoncode" id="salespersoncode" />

$("#add_user_form").validate({
  rules: {
    salespersoncode: {
      remote: {
        type: "post",
        url: "/AdminJson/CheckSalespersonCode",
        dataType: "json",
        data: {
            salespersoncode: function () { return $("#salespersoncode").val(); },
            roles: function () { return $("#roles").val(); }
        }
      }
    }
  }
}

我不想输入必填"验证选项,因为并非在所有情况下都需要salespersoncode字段.

I don't want to put in the "required" validation option because the salespersoncode field is not required in all situations.

我的问题再次出现:如何在salespersoncode字段上强制进行远程验证,无论该字段中是否包含数据?

My question again: How can I force the remote validation on the salespersoncode field whether it has data in it or not?

预先感谢

推荐答案

好像没有一个具有远程验证的form元素,而不必进行必要的验证. 最终导致依赖性不匹配.

Looks like you cannot have a form element that has remote validation without also having required validation. It ends up resulting in a dependency-mismatch.

remote方法的第一行是检查是否可选-如果可选,则返回依赖项不匹配. (可选的是远程的倒数)

The very first line in the remote method is the check to see if it is optional - if it is optional, return a dependency-mismatch. (optional is the inverse of remote)

我想出的解决方案是对必需"规则使用依赖项回调.在必需"规则的函数中,调用同步ajax调用以测试服务器上UI中的值,并返回true/false.

The solution I came up with was to use a dependency-callback for the 'required' rule. In the function for the 'required' rule, call a synchronous ajax call to test the values in the UI on the server and return true/false.

这篇关于jQuery远程验证-验证数据是否在字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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