jquery validation: prevent form submit

alex picture alex · Apr 24, 2012 · Viewed 73.9k times · Source

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?

Answer

The Alpha picture The Alpha · Apr 24, 2012

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;
}