3

I am using asp mvc model binding to bind a model that has objects in it. So

class SuperModel{
public ObjectA{get;set;}
}

Then in my view I am using @Html.TextBoxFor(model >= SuperModel.ObjectA.SomeProperty).

My issue is that I am using JQuery form validation, and as you know, TextBoxFor will auto generate a name of ObjectA.SomeProperty, which is what needs to happen so model binding works, but in my jquery validation code, I have:

  form.validate({               
            rules: {                   
                ObjectA.FName: {//INVALID BECAUSE OF PERIOD OBVIOUSLLY
                    minlength: 5,
                    required: true
                },

So I need Jquery validation to work on a html field that has a name that has a period in it. How would I go about doing this? Or is there a better way. Thank you!

2 Answers 2

5

What if you put quotes around the input names in the rule definitions?

form.validate({               
         rules: {                   
             'ObjectA.FName': {//added quotes
                 minlength: 5,
                 required: true
             },

May need to escape the '.' in the input name => 'ObjectA\.FName': {...}

NB: I would put this as a comment, but I don't have the rep yet for comments.

Sign up to request clarification or add additional context in comments.

4 Comments

Try adding two backslashes; 'ObjectA\\.FName' I just read that the first \ is interpreted by JavaScript, the second is interpreted by jQuery.
Nevermind, for some reason, even though I ALWAYS hit ctrl+R to clear the cache, the changes weren't clearing for whatever reason. This happens more often than I'd like. 'ObjectA.FName' worked fine. Thank you so much!
No worries; I was going to comment "are you sure the cache cleared?" I've had fun with that myself a few times today. ;-) Cheers!
@JohnEdwards Quick follow-up, the reason that 'ObjectA.FName' works is you likely have a version of Microsoft jQuery Unobtrusive Validation which handles escaping the '.'
1

emgee's solution worked well for me after a day of solving this. I'm using mvc4/razor and distributed jQuery Validation Plugin 1.11.1

 @Html.TextBoxFor(m => m.PersonnelBio.Firstname, new {  @class = "classname" })

jquery code:

           form1.validate({ 
            rules: {
                'PersonnelBio.Firstname': {
                    minlength: 2,
                    required: true
                },...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.