views:

97

answers:

2

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?

A: 

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.

Adrian Grigore
Various results I have encountered so far:-Entering just a 2 number account number sometimes is seen as valid and the form is sent to controller to be processed.-Entering an account number (as above) and submitting does not produce any validation messages, but does not submit the form (invalid)-Once account number field is deemed invalid, it does not become valid once the min length is met.-When form field are all valid prior to submission, the form does not submit and produces no messages.-etc. etc.This is an asp.net MVC app., but the only field in the model is the account number......
JW
+2  A: 

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 "message**s**"
*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!

JW
JW
Well, that's the kind of mistakes you make when hand-coding your validation logic on the client-side. And it's the reason why I prefer not to do it. I'm sorry that you feel I am just trying to push my blog and there are also many other articles on xVal if you don't like mine. But I still genuinely think that you would be better off using a framework like xVal for client-side validation.
Adrian Grigore
Adrian, In most cases (and in my past), I would have totally agreed, but in this case I just need to check a single value against the model and then pass the other values to a vendor's application. Additionally, why would we waste our (as developers) bandwidth to validate a few fields when we can pass it off to the client and we have an awesome tool like jQuery and the validate plugin? While I enjoyed the insight contained within your blog post, I found it to be overkill for the solution I am trying to implement, and in reality, ASP.net MVC is overkill too. I just wanted to play with it.
JW