Reputation: 690
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.
Upvotes: 0
Views: 1446
Reputation: 10466
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>
Upvotes: 2
Reputation: 16115
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.
Upvotes: 2
Reputation: 75083
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.
Upvotes: 2