maxlength 的文本输入未使用 jQuery Validate 进行验证

发布于 2024-09-12 04:59:15 字数 470 浏览 3 评论 0原文

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

示例 HTML:

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

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

那么,有人吗?谢谢!

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

Sample HTML:

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

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

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.

So, anyone? Thanks!

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

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

发布评论

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

评论(3

淡紫姑娘! 2024-09-19 04:59:15

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

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

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;
});
木緿 2024-09-19 04:59:15

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

我的解决方案只是创建我自己的 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.");

完成后,您可以添加规则,如下所示:

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

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).

//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
        }
 }
花辞树 2024-09-19 04:59:15

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

其次,您是否尝试过明确地将字段设置为不需要的字段,但在验证器对象中设置最大长度?例如:

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

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

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:

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

$('#formId').validate({rules:rules});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文