9

So I have a seven part form where six tabs are hidden via CSS and when a user clicks next another part fades in.

I've submitted the page to make sure the plugin is working; it is. Essentially its just going to the next part of the form without validation the current part

As an example

HTML

<form id = "form1" action = "backend.php" method = "post">
<label for "phone">First Name </label>
    <input type = "text" name = "fName" placeholder = "First Name..." class = "required" id = "fName" />
<button id = "btn" type = "button" class = "buttonnav next">next</button>
</form>

Javascript

$('#btn').click( function() { $("#form1").validate();});
1
  • Are you using the jQuery Validate plugin? That would be something to mention. Commented Mar 15, 2013 at 18:08

1 Answer 1

21

If you're using the jQuery Validate plugin, with your code, the plugin is not initialized until after the button is clicked. If the plugin is not initialized, then there's no validation.

$('#btn').click( function() { $("#form1").validate();});

Instead, .validate() gets called once on DOM ready to initialize the plugin on the form.

If you want to test the form at any time after initialization, you would use the .valid() method.

$(document).ready(function () {

    $('#form1').validate({ // initialize plugin
        // your options
    });

    $('#btn').click( function() { 
        $("#form1").valid();  // test the form for validity
    });

});

I have no idea how you've set up each part to to interact with each other or what other code is being used. Based on your OP alone, here is a working example using my answer above.

DEMO: http://jsfiddle.net/pqVJM/

$(document).ready(function () {

    $('#form1').validate();

    $('#btn').click(function () {
        if ($("#form1").valid()) {
            alert('hello - valid form');
        }
    });

});
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I am using the validation plugin. I edited my script to this; unfortunately I'm still getting the same result as before. Dont know if this helps or not(or shows my ignorance...), I tried if ($("#form1").valid() == true){alert("hello");} inside the click handler to see if anything was going through, and was getting constant "hello"
@ArtTaylor, I have no idea where else you're going wrong... you have not shown enough code in your OP.
@ArtTaylor, See my edited answer. With my answer applied, your code is working: jsfiddle.net/pqVJM
Thank you for your input, I got it this morning. I really appreciate the help guys.

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.