html5 form validation, void form action and execute jQuery when all html5 form elements are validated?
According to this: Do any browsers yet support HTML5's checkValidity() method?, and this may not be the latest truth since HTML5 is a work in progress, the Form.checkValidity()
and element.validity.valid
should let you access validation information from JavaScript. Assuming that's true, your jQuery would need to attach itself to the form submit and make use of that:
$('#membershipform').submit(function(event){
// cancels the form submission
event.preventDefault();
// do whatever you want here
});
You can use html5 form validation from javascript, only call to method reportValidity()
In your example:
<script>
document.querySelector('#membershipform').reportValidity()
</script>
reportValidity run html5 form validation and return true/false.
Use:
$('#membershipform').submit(function(){
// do whatever you want here
return false;
});
This is a subtle leverage of default behaviour and javascript to create what you want.
var form = document.getElementById("purchaseForm");
if(form.checkValidity()){
console.log("valid");
e.preventDefault();
}else{
console.log("not valid");
return;
}
If the form is valid then preventDefault() and continue with your function (e.g. send over ajax). If not valid then return without preventing default, you should find the default actions for form validation occurs on your form inputs.