Posted on March 15, 2021, 8 p.m. by Bishal ( 9815)
onsubmit
to call a function and the function will check if the radio button/ checkbox is checked or not.<form onsubmit="function()">
<input type="text" placeholder="text">
<input type="submit">
</form>
index.html
index.html
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Radio button - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body>
<!-- code here -->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</body>
</html>
script.js
<head> </head>
of your HTML document:<script src="script.js"></script>
script.js
file:function check() {
if (document.getElementById('agree').checked == false) {
// sweetalert code here
return false;
} else {
return true;
}
}
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<title>Radio button - Code With Bishal</title>
</head>
<!-- created by Code With Bishal - www.codewithbishal.com
-->
<body>
<form action="#" method="POST" onsubmit="return check()">
<div class="form-check">
<input type="checkbox" class="form-check-input" name="iagree" id="agree">
<label class="form-check-label" for="agree">I accept the <a href="#">terms and conditions</a>
and <a href="#">privacy policy</a></label>
<br>
<button type="submit" class="btn btn-secondary">Submit </button>
</div>
</form>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</body>
</html>
function check() {
if (document.getElementById('agree').checked == false) {
swal ("Warning", "Please indicate that you accept the Terms and Conditions and Privacy Policy", "error");
return false;
} else {
return true;
}
}