Search code examples
jqueryvalidationjquery-validation-engine

conditional validation in jquery validation engine


I have a checkbox and a textbox. I require that if a checkbox is ticked then textbox becomes compulsary. Vice versa if the textbox is filled the checkbox becomes compulsary.

So sample would be

Name of Item - Checkbox - Expiry Date Name of Item2 - Checkbox2 - Expiry Date2

Data is fetched from the database in the array using a query.

My code looks like

<script type="text/javascript"> 
$(document).ready(function(){
    $("#sea").validationEngine();
   });

function industry(field, rules, i, options) {    
var atLeastOneIsChecked = $('input[name="extraqualification[]"]:checked').length > 0;
if (atLeastOneIsChecked == 1 )        {
if(document.getElementById('extraqualificationexpirydate<?php echo $extraqual['Id']?>').value=='')
{
   return "Please supply a certificate expiry";
}else{} }
    } </script>

Solution

  • You need conditional required validation. There is a condRequired method that you can use as follows:

    <input value="" type="text" name="test" id="test" />
    <input class="validate[condRequired[test]]" type="text" id="test_2" name="test_2"/>
    

    Also, make sure you are using version 2.6 or above.