I am trying to have a checkbox and a textfield, once the checkbox is checked, the field has to be required, I am trying to do this with jQuery validate plugin using a custom method as shown below:
$(function(){
$.validator.addMethod('ifIsCrime',function(value, element){
return $("#chckCrime").is(':checked');},'This is a required field');
$('form').validate({
rules: {
txtJustification:{
required: true,
},
}
});
});
<!DOCTYPE html>
<html>
<head>
<meta name="WebPartPageExpansion" content="full" />
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
</head>
<body>
<div class="container-fluid" id="container">
<form>
<fieldset>
<legend>Personal Information</legend>
<div class="form-group">
<label for="chckCrime">Have you even been convicted of a crime? If so, please provide details</label>
<input type="checkbox" id="chckCrime" class="checkbox" name="chckCrime" />
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input class="form-control" type="text" id="txtJustification" placeholder="Click here to enter text" name="txtJustification" />
</div>
</div>
</div>
<div class="row submitrow">
<div class="col-sm-12">
<input type="submit" value="Submit My Application" class="btn btn-primary pull-right">
</div>
</div>
</fieldset>
</form>
</div><!-- container-->
</body>
</html>
Any help would be great.