jquery validation: prevent form submit
By the use of the jQuery plugin Validation, I am trying to, well, validate my form. When the validation passes, I want a javascript to fire with values from the form and then stop. no actual form submission with browsing to a new/same site.
is this possible with jquery validation?
You may try this (Example):
$(function(){
$("#myform").validate();
$("#myform").on('submit', function(e) {
var isvalid = $("#myform").valid();
if (isvalid) {
e.preventDefault();
alert(getvalues("myform"));
}
});
});
function getvalues(f)
{
var form=$("#"+f);
var str='';
$("input:not('input:submit')", form).each(function(i){
str+='\n'+$(this).prop('name')+': '+$(this).val();
});
return str;
}
Yes:
$("form").submit(function(event) {
if (somethingIsInvalid) {
event.preventDefault();
}
});