1

I have a multi-page form using jQuery Validate.

On page 1, I have a select dropdown that when someone selects something, it loads rules for the other input fields on the same page. My problem is that once the user submits and goes to page 2, and decides he needs to change something and.. he goes back to page 1 and some of the rules aren't being applied so the only way is to re-select the selector to reapply these jquery validate rules..

I need to ensure that the rules are being reapplied on page reload...

I hope this makes sense and hope someone has a simple quick answer.

1
  • Need some code.... Either use Cookies, or use a GET attribute to store the ruleset in? Commented Sep 12, 2011 at 13:32

3 Answers 3

0

ASP.NET MVC and partial client side validation

Take a look on it.

You can call programatically to $('#myform').validate(), and $('#myform').valid() to know if the form is valid and move to the next step.

$(".stepButton").live("click", function() {
    if (isValid(this)) {
        // code to proceed to next step
    }
});

function isValid(el) {
    var $thisform = $(el).parent().parent();
    $thisform.validate();
    return $thisform.valid();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could save the selected option in a php session on page 2:

session_start();
$_SESSION['yoursession'] = $_POST['option'];

On page 1 you add a variable that saves the sessions in your javascript. Use json_encode() to properly escape it:

<script type="text/javascript">
    <?php echo 'var option = "'.json_encode($_SESSION['yoursession']).'";';
</script>

So if you go to page 2 the session is set with the option that has been selected on page 1 and if you go back the content of the session is saved in the variable.

Comments

0

You shall use 2 forms, each one with a different id and declare a set of validation rules for each one. Try to adapt your code to this:

    var validationRules1 = {
        rules: {
            ...
        },
        messages: {
            ...
        }
    };

    var validationRules2 = {
        rules: {
            ...
        },
        messages: {
            ...
        }
    };

    var form1 = $('#form1');
    form1.submit(function () {
        form.validate(validationRules1);
        return form1.valid();
    });


    var form2 = $('#form2');
    form2.submit(function () {
        form.validate(validationRules2);
        return form2.valid();
    });

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.