简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM