Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate - valid if hidden field has a value

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?

like image 707
Mark Steggles Avatar asked Sep 15 '11 11:09

Mark Steggles


People also ask

How do you validate a hidden field?

Specifying hidden form field validation. To specify hidden field validation, you do the following: Create one HTML input element or CFML cfinput tag of type="hidden" for each validation rule. Specify the name of the field to validate as the first part of the hidden field name.

What is valid () in jquery?

valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.


2 Answers

Add the class "required" to the hidden input.

like image 194
svnv Avatar answered Oct 14 '22 15:10

svnv


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: []
});
like image 26
Pouki Avatar answered Oct 14 '22 14:10

Pouki