MVC2 and jquery.validate.js
I am experiencing some confusion with jquery.validate.js
First of all, what is MicrosoftMvcJqueryValidation.js. It is referenced in snippets on the web but appears to have dissapeared from the RTM MVC2 and is now in futures. Do I need it?
The reason I'm asking is that I'm trying to use the validator with MVC and I can't get it to work. I defined my JS as:
$(document).ready(function () {
$("#myForm").validate({
rules: {
f_fullname: { required: true },
f_email: { required: true, email: true },
f_email_c: { equalTo: "#f_email" },
f_password: { required: true, minlength: 6 },
f_password_c: { equalTo: "#f_password" }
},
messages: {
f_fullname: { required: "* required" },
f_email: { required: "* required", email: "* not a valid email address" },
f_email_c: { equalTo: "* does not match the other email" },
f_password: { required: "* required", minlength: "password must be at least 6 characters long" },
f_password_c: { equalTo: "* does not match the other email" }
}
});
});
and my form on the view:
<% using (Html.BeginForm("CreateNew", "Account", FormMethod.Post, new { id = "myForm" }))
{ %>
<fieldset>
<label for="f_fullname">name:</label><input id="f_fullname"/>
<label for="f_email"><input id="f_email"/>
...etc...
<input type="submit" value="Create" id="f_submit"/>
</fieldset>
开发者_开发百科<% } %>
and the validation method gets called on .ready() with no errors in firebug. however when I submit the form, nothing gets validated and the form gets submitted. If I create a submitHandler() it gets called, but the plugin doesn't detect any validation errors (.valid() == true)
What am I missing?
The MVC/jQuery validation glue file is located in the MVCFutures section of the source code. You can get the code at CodePlex. You shouldn't need it unless you are planning to use the DataAnnotations attributes with your models. The problem that you are having is likely due to not having names on your inputs as well as ids. I believe that the jQuery validation plugin uses names, not ids to match with the rules.
精彩评论