Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery form validation/Asp MVC Model Binding - Having period in Name

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!

like image 457
John Edwards Avatar asked Jan 28 '26 14:01

John Edwards


2 Answers

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.

like image 153
emgee Avatar answered Jan 30 '26 05:01

emgee


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
                },...
like image 30
kurdapya Avatar answered Jan 30 '26 05:01

kurdapya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!