0

I'm trying to test a very basic jquery validation example. the code is as follows:

<form action="/" method="post">
    <input type="text" name="foo" id="foo"/>
</form>​

My JS code:

$('#foo').validate({
    foo: {
        required: true,
        minlength: 2
    }
});​

That doesn't work... Any help or working examples will be appreciated.

1
  • Aren't you supposed to do the .validate() call on the <form>, and name the properties of the validation configuration after the field names? Your field is called "foo", not "name". Commented Apr 29, 2012 at 12:48

3 Answers 3

2

You use the validate method on your form, not in a single element, and all the rules should be inside the rules property, like:

$("form").validate({
    rules: {
        foo: {
            required: true,
            minlength: 2
        }
    }
});

a live example can be found in JsBin.

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

Comments

2

Call the validate method at the form and wrap the rules in a rules obejct. If you want add an messages object.

HTML:

<form id="form" action="/" method="post">
    <input type="text" name="foo" id="foo">
</form>

javascript:

$('#form').validate({
    rules: {
        foo: {
            required: true,
            minlength: 2
        }
    },
    messages: {
       foo: "Please enter your foo"
   }
});

Also see this example.

Comments

2

Try this

 <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>

<script> 
    $(document).ready(function(){
        $("#commentForm").validate({
        rules: {
            txtPhNumber: "required",
            txtPhNumber2:
                {
            equalTo: "#txtPhNumber"
                }
            }
        });
    });

  </script>

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.