I need to be able to conditionally require certain fields to be validated, based on the contents of a specific select field. For example, I have the following select:
<select name="new_jobType">
<option value="">Please select</option>
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
</select>
If ‘option 4’ is selected, I need to include 5-6 other select fields to be validated.
Any help would be greatly appreciated.
I might do something like this:
Just push any validation routines that need to run onto that validations array, and then on submit:
And you would have function(s) to do the actual validations:
The
eval()inside the for loop will execute the validation routine(s) based on what had been pushed onto thevalidationsarray.That way you can segment your validations based on what conditions had been met, and you don’t have to write one monolithic validation function that would be more error-prone.
Encapsulate all the things!! 🙂