knocked loose
knocked loose

Reputation: 3304

Getting around naming conventions on inputs?

I have a set of inputs that I'm using jQuery validate on, and they all base their 'error-logs' off of their names.

This wouldn't be a problem, except most of the names are set to fit with our DB so they can be uploaded on submit.

For example, I have these naming rules right now, which work great for 'first name' and 'email'

rules: {    
    firstname: {
        required: true,
    },
    email: {
        required: true,
    }
},
messages: {
    firstname: "Please enter your name",
    email: "A valid email is required",
}

But whenever I have a name like this input:

<input id="edit-submitted-constituent-base-total-constituents" class="form-text hs-input" name="submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database" required="required" size="60" maxlength="128" type="number" value="" placeholder="">

It gets a little tricky because it won't register submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database as a name and let me change the error.

My only two thoughts were to add a name="" onto the input, which doesn't work, and then somehow trying to figure out how to call to it by an id or some sort. Has anyone accomplished this?

Upvotes: 0

Views: 33

Answers (1)

Barmar
Barmar

Reputation: 780798

You can put quotes around property names in Javascript object literals when the name is not a valid identifier:

rules: {
    "submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database": {
        required: true
    }
},
messages: {
    "submitted[constituent_base][total_constituents] total_number_of_constituents_in_your_database": "You have to fill in this field"
}

Note that jquery-validate automatically detects the required attribute in the <input> element, so you can leave that out of the rules and just use this for the messages.

Upvotes: 2

Related Questions