Reputation: 61
I am building my first ASP.net MVC application (not my first jQuery & jQuery Validation plugin application), and I am having a terrible time performing the client side validation with the validation plugin. Does anyone know if there is a problem using the plugin with jQuery-1.3.2.min that comes with VS 2008?
My code is as follows:
var v = $("#frmAccount").validate({
rules: {
txtAccount: { minLength: 5,required: true },
txtFName: "required",
txtLName: "required"
},
message: {
txtAccount: {
required: "Account Number is required.",
minLength: jQuery.format("Account Number must be {0} or more charaters.")
},
txtFName: "First Name is required.",
txtLName: "Last Name is required.",
}
}); //validate
$("#cmd").click(function() {
if (v.form()) {
$("#frmAccount").submit();
}
});
............................
<form method="post" action="/Home/checkAccount" id="frmAccount">
<fieldset>
<legend>Account Information</legend>
<p>
<br />
<label for="txtAccount">Account Number:</label>
<br />
<input id="txtAccount" name="txtAccount" type="text" value="" />
<br />
<label for="txtFName">First Name:</label>
<br />
<input id="txtFName" name="txtFName" type="text" value="" />
<br />
<label for="txtLName">Last Name:</label>
<br />
<input id="txtLName" name="txtLName" type="text" value="" />
<br />
<label for="txtLName">Email:</label>
<br />
<input id="txtEmail" name="txtEmail" type="text" value="" />
</p>
<p>
<input type="button" id="cmd" name="cmd" value="Add" />
</p>
</fieldset>
</form>
When I try to submit the blank form, it returns the default "this field is required" next to the account, fname, & lname fields, but if I input any data into the fields I get various results.
I've tried to just perform the basic validate function (not returning the validate object to the v variable), but that produced similar results.
Have I just overlooked something silly here?
Upvotes: 1
Views: 1388
Reputation: 33318
jquery.validate does work fine with jquery 1.32. What do you mean by "various results"?
In any case, since you are using ASP.NET MVC, I'd recommend not coding client-side validation by hand. It's much easier, more stable and fully testable if you use xVal and the DataAnnoationModelBinder instead. Have a look at this article on client-side validation with ASP.NET MVC.
Upvotes: 0
Reputation: 61
Adrian Grigore, thanks for your quick response. However, I think that you might have overlooked the code posted above or something. Maybe you were too busy trying to plug you own blog or something. IDK.
The REAL issues are as follows:
*In the code above "message" should have been "messages"
*Also, I made (what I consider) the basic MS programming background mistake... I camelCased the work minLength, and (of course) in jQuery it should be minlength
Adrian, good luck on the blog!
Upvotes: 5