0

I'm trying to validate a form with jQuery, using the validation plugin, but can't seem to work out how to set multiple rules, for validating more than one field.

Here is my test script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){       
        $("#test").click(function () {              
            $('#test_form').validate({
                rules: {
                    firstName: {
                        required: true
                    },
                    surname: {
                        required: true
                    }
                },
                success: function() {
                    alert('it works?');
                }
            });
        });
    });
</script>
</head>
<body>
<form id="test_form" action="" method="post">
    <fieldset>
        <legend>Create an Account</legend>
        <label for="firstName">First Name</label><br/>
        <input id="firstName" name="firstName" type="text" /><br/><br/>
        <label for="surname">Surname</label><br/>
        <input id="surname" name="surname" type="text" /><br/><br/>
        <a href="#" id="test">try me</a>
    </fieldset>
</form>
</body>
</html>

Any ideas of how I've managed to mess it up, it would be most appreciated!!

Thanks!

3
  • You do not enclose .validate() within a .click() because the plugin already handles the form submission event. Also, fields are required: true by default, so no need to specify each one. Commented May 2, 2012 at 18:36
  • @Sparky672 - All fields are not default required: true, but otherwise I agree with your comment. Commented May 2, 2012 at 20:37
  • @Ryley, yes, my mistake, you are correct. So ignore my second sentence, but the OP can avoid declaring all those required: true rules by adding class='required' to his fields. Commented May 2, 2012 at 20:53

2 Answers 2

1

You need to call $("#test_form").validate(...) before the link is clicked. What's happening right now is when the link is clicked, the validation is added to the form, but not tested.

Try this:

var $form = $('#test_form');

$form.validate({
    rules: {
        firstName: {
            required: true
        },
        surname: {
            required: true
        }
    },
    success: function() {
        alert('it works?');
    }
});

$("#test").click(function(e) {
    e.preventDefault();
    $form.submit();
});
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function () {
    $('#Invoice').validate( {
        rules: {
            code: {
                required: true,
                int: true
            },

            price: {
                required: true,
                int: true
            }
        }
    });
});

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.