Reputation: 3
I have a form that should not be submitted until the user has ticked the terms and conditions checkbox (chkTerms
). What is the best way to prevent submission of the form if it is unticked?
I would have put a call to a JavaScript function in the form tag but for various reasons this is embedded in my master page so I assume this is a no go. Can this be done in jQuery for instance? Thanks
Upvotes: 0
Views: 1369
Reputation: 1
Problem is actually that once the button is 'enabled' all my other validation is bypassed and the form submits anyway...
Upvotes: 0
Reputation: 313
Best thing is to do it using JQuery bcoz if u disable a button and enable only on checkbox click, user may not know the button will get enabled only if the checkbox is checked.You can use the same coding which is mentioned above.
Upvotes: 0
Reputation: 5563
Disable your submit button on form load. Only enable it when checkbox is ticked and again disable it on uncheck of checkbox.
Check the example below:
<input type="submit" id="btn" disabled="true" value="Submit"/>
<input type="checkbox" id="chk" onchange="submitStatus();">Terms and Conditions</input>
and your script function should be
function submitStatus(){
if(document.getElementById('chk').checked){
document.getElementById('btn').disabled = false;
}else{
document.getElementById('btn').disabled = true;
}
}
Upvotes: 2
Reputation: 31033
u can use jquery validate
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Upvotes: 0
Reputation: 237865
Yes, in jQuery this is very easy:
$(document).ready(function(){
$('#myform').submit(function(e){ // when the form is loaded
if (!$('input[name="chkTerms"]')[0].checked) { // if the input with the name "chkTerms" is not checked
e.preventDefault(); // prevent the form submission
// provide feedback to the user in some way
}
}
});
Upvotes: 2