英文:
How to ensure at least one of the two input field is not empty in bootstrap 5 validation
问题
我正在这个表单中使用 Bootstrap 验证,对其他输入字段使用'required'属性。但是对于这两个字段,如果至少有一个不为空,则应该提交表单。
英文:
I am using bootstrap validation for other input fields in this form using 'required' attribute. but for this two fields if at least one is not empty than form should be submitted.
<form class="needs-validation" action="opportunity_handling.php" novalidate method="POST">
<div class="form-group col-md-6" >
<label>Opportunity OTC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueOTC">
</div>
</div>
<div class="form-group col-md-6" >
<label>Opportunity MRC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueMRC">
</div>
</div>
</form>
答案1
得分: 2
您可以使用jQuery验证插件来验证在提交表单之前至少有两个字段中的一个不为空。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>
<script>
$(document).ready(function () {
$('form').validate({
rules: {
valueOTC: {
required: function (element) {
return $('input[name="valueMRC"]').val() == '';
}
},
valueMRC: {
required: function (element) {
return $('input[name="valueOTC"]').val() == '';
}
}
}
});
});
</script>
您可以使用Bootstrap验证来使用data-attribute data-bv-one-field-required-by-name
验证至少有两个字段中的一个不为空。
<form class="needs-validation" action="opportunity_handling.php" method="POST" novalidate>
<div class="form-group col-md-6">
<label>Opportunity OTC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueOTC" data-bv-one-field-required-by-name="valueOTC,valueMRC">
</div>
</div>
<div class="form-group col-md-6">
<label>Opportunity MRC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueMRC" data-bv-one-field-required-by-name="valueOTC,valueMRC">
</div>
</div>
</form>
英文:
You can use the jQuery validation plugin to validate that at least one of the two fields is not empty before submitting the form.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>
<script>
$(document).ready(function () {
$('form').validate({
rules: {
valueOTC: {
required: function (element) {
return $('input[name="valueMRC"]').val() == '';
}
},
valueMRC: {
required: function (element) {
return $('input[name="valueOTC"]').val() == '';
}
}
}
});
});
</script>
You can use the data-attribute data-bv-one-field-required-by-name to validate that at least one of the two fields is not empty using Bootstrap validation.
<form class="needs-validation" action="opportunity_handling.php" method="POST" novalidate>
<div class="form-group col-md-6" >
<label>Opportunity OTC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueOTC" data-bv-one-field-required-by-name="valueOTC,valueMRC">
</div>
</div>
<div class="form-group col-md-6" >
<label>Opportunity MRC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueMRC" data-bv-one-field-required-by-name="valueOTC,valueMRC">
</div>
</div>
</form>
答案2
得分: 1
你可以在两个字段上都使用HTML的"required"属性,然后在表单中添加一个自定义的验证规则。这个自定义验证规则将检查这两个字段中至少有一个不为空。
<form class="needs-validation" action="opportunity_handling.php" method="POST" novalidate>
<div class="form-group col-md-6">
<label>Opportunity OTC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueOTC" required>
</div>
</div>
<div class="form-group col-md-6">
<label>Opportunity MRC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueMRC" required>
</div>
</div>
</form>
<script>
$(document).ready(function () {
window.addEventListener('load', function () {
// 获取所有要应用自定义Bootstrap验证样式的表单
var forms = document.getElementsByClassName('needs-validation');
// 循环遍历它们并阻止提交
var validation = Array.prototype.filter.call(forms, function (form) {
form.addEventListener('submit', function (event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
// 添加自定义验证规则
if ($('input[name="valueOTC"]').val() == '' && $('input[name="valueMRC"]').val() == '') {
event.preventDefault();
event.stopPropagation();
$('#error').html('<div class="alert alert-danger">请填写机会OTC价值或机会MRC价值中的至少一个。</div>');
}
form.classList.add('was-validated');
}, false);
});
}, false);
});
</script>
英文:
You can use the HTML required attribute on both fields and then add a custom validation rule to the form. This custom validation rule will check if at least one of the two fields is not empty.
<form class="needs-validation" action="opportunity_handling.php" method="POST" novalidate>
<div class="form-group col-md-6" >
<label>Opportunity OTC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueOTC" required>
</div>
</div>
<div class="form-group col-md-6" >
<label>Opportunity MRC Value:</label>
<div class="input-group">
<input type="number" class="form-control" name="valueMRC" required>
</div>
</div>
</form>
-
<script>
$(document).ready(function () {
window.addEventListener('load', function () {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function (form) {
form.addEventListener('submit', function (event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
//add custom validation rule
if ($('input[name="valueOTC"]').val() == '' && $('input[name="valueMRC"]').val() == '') {
event.preventDefault();
event.stopPropagation();
$('#error').html('<div class="alert alert-danger">Please enter either Opportunity OTC Value or Opportunity MRC Value.</div>');
}
form.classList.add('was-validated');
}, false);
});
}, false);
});
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论