I am using JavaScript and Bootstrap for validation in my form. Everything works the way it should when I make all of these 4 fields required.
However, I need an either/or situation. For example, if the first two fields, 'name' and 'SIN' are filled, then 'BusinessName' and 'BusinessID' are not required and vice versa. Here is the current code:
<div class="form-group">
<label class="col-lg-3 control-label">Name</label>
<div class="col-lg-5">
<input type="text" required class="form-control" name="name" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">SIN #</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="SIN" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Business Name</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="BusinessName" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Business #</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="Businessid" />
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#defaultForm').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
name: {
validators: {
notEmpty: {
message: 'The Name is required and cannot be empty'
},
regexp: {
regexp: /^[a-zA-Z-\.\ ]+$/,
message: 'The Name can only consist of Alphabets, Space and Hyphens'
}
}
},
SIN: {
validators: {
notEmpty: {
message: 'The SIN is required and cannot be empty'
},
regexp: {
regexp: /^[0-9\ ]+$/,
message: 'The SIN can only consist of Numbers and Space'
}
}
},
BusinessName: {
validators: {
notEmpty: {
message: 'The Business name is required and cannot be empty'
}
}
},
Businessid: {
validators: {
notEmpty: {
message: 'The Business# is required and cannot be empty'
},
regexp: {
regexp: /^[0-9]+$/,
message: 'The Business# can only consist of Numbers'
}
}
},
des1: {
}
}
}) .on('success.form.bv', function(e) {
});
});
</script>