donk
donk

Reputation: 1570

jquery validation working on some browsers, but not on others

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

}

Upvotes: 0

Views: 280

Answers (2)

Nick Craver
Nick Craver

Reputation: 630599

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

Upvotes: 1

donk
donk

Reputation: 1570

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

Upvotes: 0

Related Questions