views:

185

answers:

2

Hi

I'm using jQuery Validate and xVal to validate in input forms. This works great. However, I do want to mark my required fields with an asterix (*) or something like that to show which field is actually required.

I could do this by hand, but since I already generate my validation rules with xVal I thought I could mark the required fields automatically based on the provided rules.

Is there a way to hook into either xVal or jQuery Validate to retrieve the rules, or somehow make them mark my required fields?

I'm using the latest versions of jQuery and jQuery Validate.

Thanks

A: 

It is probably easiest to make a separate "rules" variable which you pass to both jQuery and some other function. This other function would loop through it and mark all required fields as such.

Sjoerd
That could work, but I don't want to modify xVal to accomplish this.To generate my validation rules from my edit model, I use this line of code: Html.ClientSideValidation<NewCompanyInput>()
Henning
+2  A: 

Give the class required to the form elements, and use the following code

$(document).ready(function() {
     $('<span style="color:red;">*</span>').insertAfter('.required');
});

This will attach "*" to every elements with required class

Hope it helps

Starx
Yes, that would probably work if they had the required class, but as far as I know, xVal does not set the classes, but rather provide the rules directly to jQuery Validate.I guess the rules live in memory or something.
Henning
Would you upload some code then? I would like to see how the validation function is attached to the element.
Starx
This is the jQuery Validation code that is generated, http://gist.github.com/482877. As for xVal, you can get the code here, http://xval.codeplex.com/. I can provide a gist for that as well if you want.
Henning
xVal code is here, http://gist.github.com/482883
Henning