Reputation: 6110
I have two check boxes in my form, all other form fields use HTML attribute required so I can run validation on submit. This is little different since I have two check box input fields and I have to require at least one to be checked. Here is an example:
$('.frm-Submit').on('submit', submitFrm);
function submitFrm(e){
e.preventDefault(); // Prevnts default form submit.
var $checkbox = $('.account-type');
$checkbox.on('change', function(){
var checked = false;
$checkbox.each(function(){
checked = checked || $(this).is(':checked')
});
$checkbox.prop('required', !checked)
});
console.log('Submit form.')
}
.form-group.required .control-label:after {
content: " *";
color: red;
}
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmSave" id="frmSave" class="frm-Submit" autocomplete="off">
<div class="form-group required">
<label class="control-label" for="account"><span class="label label-primary">Account Type:</span></label>
<div class="checkbox">
<label for="user">
<input type="checkbox" name="frm_isuser" id="frm_isuser" data-toggle="collapse" data-target="#user-account" required class="account-type"> <span class="label label-default">User</span>
</label>
<label for="staff">
<input type="checkbox" name="frm_isstaff" id="frm_isstaff" data-toggle="collapse" data-target="#staff-account" required class="account-type"> <span class="label label-info">Staff</span>
</label>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-1 col-lg-1">
<button type="submit" name="frm_submit" id="frm_submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Example above throws an error isUser.checkValidity()
is not a function. Is there a way to run validity on checkbox and make at least one of the two required/checked?
Upvotes: 3
Views: 3068
Reputation: 3102
.is(":checked")
already returns a boolean
...there is no .checkValidity()
method. Just do:
if (isUser || isStaff) {
//...code here if at least one is true
}
Update:
The requirement and validation that you need in this case are essentially two separate sets of logic.
Constraint validation runs on a per input basis so you have to check multiple inputs yourself.
You have the option of swapping the value of the required
parameter by listening for events on each checkbox...but since they seem to have different data-target
, I assume the better solution would probably be just using the select form type.
Upvotes: 1
Reputation: 1598
add like this,
$(function(){
$('[type="checkbox"]').prop('required', true);
$('[type="checkbox"]').filter('[required]').change(function(){
$('[type="checkbox"]').prop('required', ($(this).is(':checked')) ? false : true);
})
$('.frm-Submit').on('submit', function(e){
e.preventDefault(); // Prevnts default form submit.
if($('[type="checkbox"]:checked').length > 0){
console.log('Submit form.')
}else{
$(this).closest('form').get(0).checkValidity();
}
});
});
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmSave" id="frmSave" class="frm-Submit" autocomplete="off">
<div class="form-group required">
<label class="control-label" for="account"><span class="label label-primary">Account Type:</span></label>
<div class="checkbox">
<label for="user">
<input type="checkbox" name="frm_isuser" id="frm_isuser" data-toggle="collapse" data-target="#user-account"> <span class="label label-default">User</span>
</label>
<label for="staff">
<input type="checkbox" name="frm_isstaff" id="frm_isstaff" data-toggle="collapse" data-target="#staff-account"> <span class="label label-info">Staff</span>
</label>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-1 col-lg-1">
<button type="submit" name="frm_submit" id="frm_submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Upvotes: 2
Reputation: 4305
$("#frm_isuser").is(":checked")
will return a bool that shows if $("#frm_isuser")
is checked or not.
And checkValidity()
is a DOM methods, so you should use document.getElementById("frm_isuser").checkValidity()
or $("#frm_isuser")[0].checkValidity()
.
Edit: If one of the both checkboxes is checked, then set required
attributes to false
to both of them. If none of them is checked, set required
to true
.
$('.frm-Submit').on('submit', submitFrm);
var $checkbox = $('.custom-class')
$checkbox.on('change', function(){
var checked = false;
$checkbox.each(function(){
checked = checked || $(this).is(':checked')
})
$checkbox.prop('required', !checked)
})
function submitFrm(e) {
e.preventDefault(); // Prevnts default form submit.
console.log('Submit form.')
}
.form-group.required .control-label:after {
content: " *";
color: red;
}
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmSave" id="frmSave" class="frm-Submit" autocomplete="off">
<div class="form-group required">
<label class="control-label" for="account"><span class="label label-primary">Account Type:</span></label>
<div class="checkbox">
<label for="user">
<input type="checkbox" name="frm_isuser" id="frm_isuser" data-toggle="collapse" data-target="#user-account" required class="custom-class"> <span class="label label-default">User</span>
</label>
<label for="staff">
<input type="checkbox" name="frm_isstaff" id="frm_isstaff" data-toggle="collapse" data-target="#staff-account" required class="custom-class"> <span class="label label-info">Staff</span>
</label>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-1 col-lg-1">
<button type="submit" name="frm_submit" id="frm_submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Upvotes: 5