9

I need to validate a field based on the value of a hidden field. I tried this custom rule:

jQuery.validator.addMethod("cityselected", function() {
    if ($('#featureID').val() == "") return false;
    else return true;
});

cityselect: {
    required: true,
    cityselected: true
},

featureID is a hidden input which I need to check has a value. This doesnt seem to work though. Any ideas?

2 Answers 2

4

Add the class "required" to the hidden input.

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

Comments

2

If your plugin version is greater than 1.9, you'll have to explicitly add hidden to the validation process, like that when you initialize the plugin :

$("#form").validate({
  // Do not ignore hidden fields
  ignore: []
});

1 Comment

You are correct... this is the way to do it with the later versions of the plugin. However, in all fairness to the OP, this was not an option in 2011 when hidden fields were not ignored.

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.