提交表单甚至返回false

I want to save this form data using ajax. Anyone help me to resolve below issue.

If I am entering all entries correct and submits it refreshes page.

And if i enter any on incorrect and submit it send error. and then if i fill all correct it works fine.

But get refresh in first case.

Below is the script

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='script/lib/jquery-1.10.2.js'></script>
<script type='text/javascript' src='script/plugin/form-validator/jquery.form-validator.min.js'></script>
<script>
$(document).ready(function(){
    $.validate({
        modules : 'location, date, security, file',             
        borderColorOnError : '#ddd',
        //errorMessagePosition : $messages,
        onModulesLoaded : function() {
            $('#country').suggestCountry();
        }
    });
});

function validate_rgn(){
    $('#registration_form').submit();
    $.validate({
     form : '#registration_form',
     onSuccess : function() { 
         alert('right');
      return false; // Will stop the submission of the form
    },
    });
}
</script>
</head>
<body>
<form action="/registration" method="POST" id="registration_form">
    <p>
        Name (4 characters minimum):
        <input name="user" data-validation="length"
               data-validation-length="min4"/>
    </p>
    <p>
        Year (yyyy-mm-dd):
        <input name="birth" data-validation="date"
               data-validation-format="yyyy-mm-dd"/>
    </p>
    <p>
        Website:
        <input name="website" data-validation="url"/>
    </p>
    <p>
        <input type="button" value="save" onclick="validate_rgn()" />
    </p>
</form>
</body>
</html>

Validation script from http://formvalidator.net

The event handler doesn't return anything:

onclick="validate_rgn()"

The function between " and " has no return statement. You call another function and ignore the return value.


validate_rgn doesn't return false. The only return statement inside it belongs to a different function, the anonymous one you pass to to onSuccess.


The form is submitted because you call $('#registration_form').submit();, there is nothing preventing that from firing.


A dirty solution would be to forget about returning anything and just move $('#registration_form').submit(); inside the onSuccess function.

A cleaner solution would be to:

  1. Use a real submit button
  2. Bind your event handler to the submit event of the form (instead of the click event of the button)
  3. Set up your validation routine so it does actually return (no idea how you would do that, it looks like it is asynchronous, I'd look at rewriting it so it wasn't)