views:

20

answers:

1

I have a form that will have dynamically created elements, one of those will be of date type. There can also be many fields of this type on my form, all which must be validated.

I am using a strongly typed view in Asp MVC, so the name will change based on various factors. What I would like to do is validate based on a class name instead, since that will be constant for that type of field.

Ie:

<%= Html.TextBox("questionAnswers[" + index + "].AnswerValue", qa.AnswerValue, new { @class = "DateTypeClass" })%> 

So then I would need JQuery validation based on the classname DateTypeClass versus the Name.

Any ideas?

A: 

In your validation function you could check the value like so:

var dateFieldValue = $(".DateTypeClass").val();
//validate dateFieldValue here

Also you don't have to resort to selecting by class if you don't want to. If no other fields share this validation then you could select by partial id in jquery. That would look like this:

var dateFieldValue = $("[id^=someId]").val();
//validate dateFieldValue here

So you would if set someId on the field and ASP.Net adds some additional stuff to the id it will still select it properly. I don't use ASP.Net MVC so not sure where you are setting the ID (if at all)

Also if you want inline validation you can wireup the onBlur event like so:

$(".DateTypeClass").blur(function(){
       //Call field validation function here
});
antonlavey