5

I have a very strange issue with the jQuery Validation plugin where it won't validate a select that was pre-filled then modified.

You can look at the code here: http://jsfiddle.net/VwRJ8/1/

The code never fires the alert() in the following lines:

$('#edit_loc_save').click(function() {
    var foo = $('#add_new_loc_form').valid();
    alert(foo);
});
4
  • The select box will validate on submit but not initially on change. Also you should put a name value for your select.
    – 3dgoo
    Commented Dec 9, 2012 at 4:15
  • I should have been more specific. It's not validating on submit.
    – RHPT
    Commented Dec 9, 2012 at 4:32
  • There might be an error in your javascript causing the validate function to never be called. Your example code works fine on submit. Here is a demo showing so: jsfiddle.net/bPcr8/5. Check your error console to see if there are any errors. Otherwise, can you share a live version of the problem for everyone?
    – 3dgoo
    Commented Dec 9, 2012 at 5:44
  • Here's a fiddle that's very close to the live code. Yo can reproduce the problem here: jsfiddle.net/VwRJ8/1
    – RHPT
    Commented Dec 10, 2012 at 0:31

3 Answers 3

5

You can check if the select is valid each time the value is changed:

Javascript

$('#myform').validate();
$('#myselectbox').on('change', function() {
    $(this).valid();
});

Demo

Edit 1

Here is what is causing an error in your javascript, which is preventing the validate function to operate correctly:

new_loc_zip: {
    required: true,
    postalcode: true // This is not a correct setting in jquery validate
},

If you remove the postalcode: true line, validate will call properly.

Demo

4
  • I have a custom validation rule for postalcode that I forgot to include :\ Would that still cause an issue?
    – RHPT
    Commented Dec 10, 2012 at 3:16
  • jsfiddle.net/VwRJ8/6 there doesn't seem to be an issue with the validation rule in. Select is being validated correctly.
    – 3dgoo
    Commented Dec 10, 2012 at 3:34
  • It is bizaare that I can't get it to work on the actual code (It's inside a corporate network so I can't show it). I do have multiple forms with separate validation rules. I wonder if that's the issue.
    – RHPT
    Commented Dec 10, 2012 at 4:20
  • Bizarre. Good luck. My tip would be to view your error console as you submit the form and see if there are any errors. If you are able to recreate the problem in the fiddle we can try to help you. Otherwise, good luck.
    – 3dgoo
    Commented Dec 10, 2012 at 4:24
1

if you want to do the validation while changing the option, then try to handle the change event of the select option and trigger the validation programatically

Try something like this

$(document).ready(function() {
        $('#myselectbox').change(function() {
              $('#myform').validate({
                            rules: {
                              myselectbox: {required: true }
                            }
                          }).form();
                       });
                    });​

See the fiddle : here

1
1

http://jsfiddle.net/YRAga/2/

$('#myform').validate({
    rules: {
        myselectbox: {
            required: true
        }
    }
});

$("#myselectbox").change(function() {
    $("#myform").submit();
});​

Not the answer you're looking for? Browse other questions tagged or ask your own question.