jquery 验证适用于某些浏览器,但不适用于其他浏览器

发布于 2024-10-09 00:33:26 字数 1333 浏览 0 评论 0原文

为什么这对某些人有效,而对其他人无效?其他人只是点击按钮,不起作用。在我添加 nospaces 验证方法后它就开始这样做了。任何建议将不胜感激。

<form method="post" action="#" id="client_form" class="order-form">
<input type="text" id="user_name" name="user_name" class="req nospaces" />
</form>
 <button id="client_next_btn"><?php echo($content->getString('order')); ?></button>
<script type="text/javascript">

$(function ()
{
jQuery.validator.addClassRules('req', { required: function (el) { return $(el).is(':visible') } });

var nums = ['0','1','2','3','4','5','6','7','8','9'];
var letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'];
var alphanumeric = nums.concat(letters);

jQuery.validator.addMethod("nospaces", function(value, element) {
    var val = value.split("");
    var bool = true;
    for (var i in val)
    {
        if (jQuery.inArray(val[i], alphanumeric) == -1)
        {
            bool = false;
            break;
        }
    }
    return bool;
}, "* <?php echo ($content->getString('no_spaces')); ?>");

$('#client_form').validate();
$('#client_next_btn').click(function ()
{
    $('#client_message').empty();

    if ($('#client_form').validate().form())
    {
                alert('works');
            }
    }

}

Why does this work for some people, and doesn't for others? Others just click the button and it doesn't work. It started doing that after I added the nospaces validation method. Any suggestions would be very much appreciated.

<form method="post" action="#" id="client_form" class="order-form">
<input type="text" id="user_name" name="user_name" class="req nospaces" />
</form>
 <button id="client_next_btn"><?php echo($content->getString('order')); ?></button>
<script type="text/javascript">

$(function ()
{
jQuery.validator.addClassRules('req', { required: function (el) { return $(el).is(':visible') } });

var nums = ['0','1','2','3','4','5','6','7','8','9'];
var letters = ['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'];
var alphanumeric = nums.concat(letters);

jQuery.validator.addMethod("nospaces", function(value, element) {
    var val = value.split("");
    var bool = true;
    for (var i in val)
    {
        if (jQuery.inArray(val[i], alphanumeric) == -1)
        {
            bool = false;
            break;
        }
    }
    return bool;
}, "* <?php echo ($content->getString('no_spaces')); ?>");

$('#client_form').validate();
$('#client_next_btn').click(function ()
{
    $('#client_message').empty();

    if ($('#client_form').validate().form())
    {
                alert('works');
            }
    }

}

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

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

发布评论

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

评论(2

月下伊人醉 2024-10-16 00:33:26

作为提示,您可以摆脱这个:

jQuery.validator.addClassRules('req', { 
  required: function (el) { return $(el).is(':visible') } 
});

并使用内置的 required 类,只需添加 ignore选项到您的.validate() 调用排除 :hidden 元素,如下所示:

$('#client_form').validate({ ignore: ':hidden' });

Just as a tip, you can get rid of this:

jQuery.validator.addClassRules('req', { 
  required: function (el) { return $(el).is(':visible') } 
});

and use the built in required class, just add the ignore option to your .validate() call to exclude :hidden elements, like this:

$('#client_form').validate({ ignore: ':hidden' });
望她远 2024-10-16 00:33:26

就在我发布问题后,我找到了答案 - addMethod 必须与 addClassRules 一起使用,而不是单独使用

Just after I posted the question, I figured out the answer - addMethod has to go together with addClassRules and not by itself

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