1

I'm having trouble getting jquery.validation.js to validate a select box. It is working on the text inputs, but it does not display an error message for the select. If I fulfill validation on the text inputs, it submits. Does anyone see what I'm missing here?

<form name="consumerRegistration" id="consumerRegistration" action="" method="post">
    <label for="name" class="baseline-10">Full Name</label>
    <input type="text" name="name" id="name" class="required" placeholder="John Doe" min-length="2"/>
    <label for="email" class="baseline-10">Email</label>
    <input type="text" name="email" id="email" class="required email" placeholder="[email protected]"/>
    <label for="company" class="baseline-10">Company</label>
    <input type="text" name="company" id="company" placeholder="ie. Yahoo"/>
    <select name="country" id="country" class="required" required="required">
        <option selected="" val="" disabled="disabled">Country</option>
        <option val="United States">United States</option>
        <option val="Canada">Canada</option>
    </select>
    <a href="javascript:document.void(1);" title="Register" class="cta"><span>Register</span></a>

(function($){
if($('form#consumerRegistration').length){
    $('input').placeholder();
    $('form#consumerRegistration').find('a').on('click', function(e){
        e.preventDefault();
        if($('form#consumerRegistration').valid()){
            $('form#consumerRegistration').submit();
        } else {
            return false;
        }
    });
}
})(jQuery);

If I've missed a solution somewhere else in the site, I swear I've looked thouroughly. :)

The plugin is from http://bassistance.de/jquery-plugins/jquery-plugin-validation/ .

3
  • Well, I can't tell what's wrong without seeing the code from jquery.validation.js. So maybe you should add that to your question. Commented Feb 28, 2012 at 0:19
  • Sorry about that, I've added a link to the plugin homepage, but I didn't include the code here because it seemed like 1100 lines might be overkill. Commented Feb 28, 2012 at 0:33
  • thanks for the cleanup Glenn ;] Commented Feb 28, 2012 at 0:51

2 Answers 2

2

OK, your problem is that you're not really using jquery validate correctly. What you need to do is more like this:

First, setup the validation on document.ready, if you don't do this, it doesn't validate the form any other way:

$('form#consumerRegistration').validate(...//options here

Next, create a click event for your a elements that just submits the form (validate will then do its thing)

$('form#consumerRegistration').find('a').on('click', function(e){
    $('form#consumerRegistration').submit();
});

Finally, the reason your select isn't being dealt with correctly is that you haven't specified its attributes correctly. Simply, val is not the right attribute for your options, value is. So just change that in all the options:

    <option selected="" value="" disabled="disabled">Country</option>
    <option value="United States">United States</option>
    <option value="Canada">Canada</option>

Here it is working: http://jsfiddle.net/ryleyb/TTpSB/

Sign up to request clarification or add additional context in comments.

2 Comments

I'm smacking myself right now, I can't beleive I effed with this so long and didn't catch the mis named attribute. Thanks for figuring this out, I'll give the other fixes a try as well.
^5. I'm an idiot for the "val" issue, but I'm glad it prevented validation, cause it works with the correct attribute, but I'd never have realized I was mis-implementing the plugin.
0

Jquery Validation Plugin requires the first option of select to be blank to validate them. Ex: your select box should look like Following :

<select name="country" id="country" class="required">
        <option></option>
        <option val="United States">United States</option>
        <option val="Canada">Canada</option>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.