开发者

jquery validation working on some browsers, but not on others

开发者 https://www.devze.com 2023-02-01 01:54 出处:网络
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 woul

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_bt开发者_开发百科n"><?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');
            }
    }

}


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' });


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

0

精彩评论

暂无评论...
验证码 换一张
取 消