jQuery Validate插件-不评估空字段 [英] jQuery Validate plugin - don't evaluate empty fields

查看:110
本文介绍了jQuery Validate插件-不评估空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Validate插件,并且我想做的事情相对容易,但是我无法使其正常工作.

I'm using the jQuery Validate plugin and what I'm trying to do should be relatively easy, but I can't get it to work.

我有一个input字段,它不是必填/必填字段.如果未在该字段中输入任何内容,则我不想对该字段运行任何形式的验证.如果表单字段发生更改并包含我要针对其进行验证的任何输入(例如,仅数字),但是如果该字段为空白,则删除/不进行验证.

I've got an input field that is not a mandatory/required field. If nothing is entered into the field I don't want to run any form of validation on the field. If the form field changes and contains any input I want to validate it against, for example, numbers only, but if the field is blank, remove / don't validate.

我试图做这样的事情:

valForm('excessFrm');

var vou = $('input[name="voucher"]');

vou.blur(function(){
  if($(this).val() == ''){
    ( "#voucher" ).rules( "remove" );
  } else {
    ( "#voucher" ).rules( "add", {
        number: true
    });
  }
})

但是无论我在代码中的什么位置,都会出现此错误:

But no matter where I put this in my code I'm getting this error:

Uncaught TypeError: Object #voucher has no method 'rules' 

即使我确定validate()对象已启动,但valForm()函数也会调用验证对象并设置所有默认规则等.

Even though I'm sure the validate() object has been initiated, the valForm() function calls the validation object and setup all my default rules etc.

有人可以提供一些智慧的话吗?

Can anybody offer some words of wisdom please?

推荐答案

引用OP:

我输入的字段不是必填/必填字段.如果 没有输入任何内容,我不想运行任何形式的 现场验证.如果表单字段更改并包含任何 输入我想仅针对例如数字来验证它,但是如果 该字段为空白,请删除/不验证.

I've got an input field that is not a mandatory/required field. If nothing is entered into the field I don't want to run any form of validation on the field. If the form field changes and contains any input I want to validate it against, for example, numbers only, but if the field is blank, remove / don't validate.

您刚刚描述了任何表单验证软件(包括jQuery Validate插件)的预期正常行为.

You've just described the expected normal behavior of any form validation software, including the jQuery Validate plugin.

您不需要自定义的blur事件处理程序,因为该插件执行由默认启用的onfocusout选项触发的验证.

You don't need a custom blur event handler because the plugin performs validation triggered by the onfocusout option which is enabled by default.

您无需添加&使用rules()方法删除规则,因为默认情况下,该插件不会评估空白字段中的规则. (当然,唯一的例外是required规则,根据定义,该规则仅适用于空字段.)

You don't need to add & remove rules using the rules() method because the plugin, by default, will not evaluate the rules on an empty field. (Of course, the single exception is the required rule, which by definition, only applies to an empty field.)

删除代码,然后从类似这样的内容开始...

Delete your code and start over with something more like this...

HTML :

<form id="myform">  
     <input type="text" name="voucher" />
     <input type="submit" />
</form> 

jQuery :

$(document).ready(function() {

    $('#myform').validate({
        rules: {
            voucher: {
                number: true
            }
        }
    });

});

工作演示: http://jsfiddle.net/HBfJB/

如在演示中所见,将字段保留为空白即可提交,但是,当输入任何字符时,将评估验证规则.

As you can see in the demo, leaving the field blank allows submission, however, when any characters are entered, the validation rule is evaluated.

这篇关于jQuery Validate插件-不评估空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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