具有最大长度的文本输入未使用 jQuery Validate 进行验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3365276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:19:17  来源:igfitidea点击:

Text inputs with maxlength not validating with jQuery Validate

jqueryjquery-validate

提问by rdumont

I'm stuck with a problem using jQuery Validate. The thing is I have a few textboxes with their maxlengthattributes set. Although they are not required, they still fail to validate when either empty or whitespace.

我在使用 jQuery Validate 时遇到了问题。问题是我有几个maxlength设置了属性的文本框。尽管它们不是必需的,但它们仍然无法在空白或空白时进行验证。

Sample HTML:

示例 HTML:

<input type="textbox" id="tbx1" maxlength="100" value="" />

<input type="textbox" id="tbx1" maxlength="100" value="" />

Then, $('#tbx1').valid()returns false. Shouldn't it return trueinstead? Are there any known workarounds for this?

然后,$('#tbx1').valid()返回false。它不应该返回true吗?是否有任何已知的解决方法?

Oh, and adding $('#myform').validate({ ignore: '#tbx1' });doesn't quite work for me, because I'm using ASP.NET controls with their automatic IDs. I know I could either use the control's client id or something like Wilco's IDOverride, but this is just not what I prefer.

哦,添加$('#myform').validate({ ignore: '#tbx1' });对我来说不太适用,因为我使用的是带有自动 ID 的 ASP.NET 控件。我知道我可以使用控件的客户端 ID 或 Wilco 的 IDOverride 之类的东西,但这不是我喜欢的。

So, anyone? Thanks!

那么,有人吗?谢谢!

回答by Bruno

This seems to be a bug in this plugin. If fixed it by replacing the method by a custom implementation using the following code:

这似乎是这个插件中的一个错误。如果通过使用以下代码通过自定义实现替换方法来修复它:

$.validator.addMethod("maxlength", function (value, element, len) {
   return value == "" || value.length <= len;
});

回答by Chris

Ran into this problem myself on some complex forms I was writing...

我自己在写一些复杂的表格时遇到了这个问题......

My solution is just to create my own maxlength method that I can 'maxLen' that works almost exactly like the default method just under a different name. Just paste this above your main validate call ($("#mainform").validate({}); or whatever your form is called).

我的解决方案只是创建我自己的 maxlength 方法,我可以“maxLen”,它的工作方式几乎与默认方法完全相同,只是名称不同。只需将其粘贴到您的主要验证调用($("#mainform").validate({}); 或任何您的表单调用之上)。

//Custom Max Length Validation
jQuery.validator.addMethod("maxLen", function (value, element, param) {
    //console.log('element= ' + $(element).attr('name') + ' param= ' + param )
    if ($(element).val().length > param) {
        return false;
    } else {
        return true;
    }
}, "You have reached the maximum number of characters allowed for this field.");

Once you've done that you can add the rule like so:

完成后,您可以像这样添加规则:

 rules: {
       Message: {
            required: false,
            maxLen: 200
        }
 }

回答by Philip Schweiger

First, I assume that the input has a "name" attribute and is simply not shown in your example? If not, try that and it may fix the issue.

首先,我假设输入具有“名称”属性,并且在您的示例中根本没有显示?如果没有,请尝试它,它可能会解决问题。

Second, have you tried explicitly setting the field to be not required but to have a maxlength in the validator object? For instance:

其次,您是否尝试过将字段显式设置为不需要但在验证器对象中有 maxlength ?例如:

var rules = {
  'txtbx1': {
    required: false,
    maxlength: '100'
  }
};

$('#formId').validate({rules:rules});