Validating a form using JQuery validate

1

I'm here with a problem validating my form, every time I change the Country I load the page again:

<select class="form-control" id="pais" name="pais" 
        onchange="document.getElementById('form_user').action='';this.form.submit();">

I do this to add new fields if the country is Portugal.

The problem is that I'm using JQuery's validate and the validation of this and other fields disappears when I change the country, because I download the page again.

How can I keep the validation fields always working, as soon as I click at least once button ?

How do I validate:

$('#form_user').validate({
        rules: {
            nome: {
                minlength: 3,
                maxlength: 50,
                required: true
            },
            //outros campos...
            pais: {
                valueNotEquals: "0"
            }
       }
       //mensagens, etc...
 });
    
asked by anonymous 09.07.2014 / 17:49

1 answer

1

The way I found to solve the problem was to check, in PHP, if a POST was done, if yes I do a validation.

This ensures that every time you have POST the validation is done.

My script :

var validator = $('#form_user').validate({
    rules: {
        nome: {
            minlength: 3,
            maxlength: 50,
            required: true
        },
        //outros campos...
        pais: {
            valueNotEquals: "0"
        }
    }
    //mensagens, etc...
});

If there is a POST , the form is validated:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
?>
    validator.form();
<?php 
}
?>

In the case of click on the submit button, it is also validated.

$( "#submit" ).click(function() 
{
    validator.form();
});
    
10.07.2014 / 13:05