-1

Is there a work-around for the jQuery Validation plugin rules to work on form input elements with names containing leading zeros?

This works fine...

first_name: {required: true, minlength: 2},

But this locks the script...

00ND0000005Rtcx: {min: 10, digits: true},

Quotes and parentheses around the element name also lock the script and I have no choice on the form input element names - and the plugin only allows element name as the rule selector

I guess I could replicate the form, using a different set of named input elements and then updating the results into the fields of the 'real form' to be posted - but that would introduce considerable complexity.

Sparky
  • 94,381
  • 25
  • 183
  • 265
  • Typically, depending on `doctype`, starting a `name` or `id` with a number is invalid, or at the least, a bad practice. See: http://stackoverflow.com/a/79022/594235 – Sparky Dec 09 '13 at 02:44
  • Thanks for the response - I recognise that it's bad practice, but the problem here is about validating form input names. So, the names are defined by the server - not me - hence the need for a work-around :-) – Keith Clarke Dec 09 '13 at 08:29
  • I guess you didn't read the answer at the link I provided. Not only bad practice but the root cause of your problem. Obviously jQuery, JavaScript, or the validation plugin can't handle it. The solution is to fix the server code if you want to use JavaScript/jQuery. – Sparky Dec 09 '13 at 14:19

2 Answers2

1

You can make it work simply by quoting your name. You claim that this 'locks' the script, but I think you must just be doing it wrong somehow:

$('form').validate({
   rules: {
      first_name: {required: true, minlength: 2},
      '00ND0000005Rtcx': {min: 10, digits: true}
   }
});

That's all it takes!

As Sparky says, names and IDs should not start with numbers, but my experience is that all browsers can handle name attributes that start with a number, just not id.

If there is something else going wrong, it would be very helpful for you to provide more information - use a debugger to find out the exact error message. Show us the relevant parts of the form. Show us more of the relevant jQuery.

Here's a working example of something similar: http://jsfiddle.net/ryleyb/3J4bp/1/

Note that your numbered field is required and the form will not submit unless it is filled in.

Ryley
  • 20,431
  • 1
  • 65
  • 79
  • Sorry, my mistake, putting the name in quotes in the rules parameter block doesn't lock the script - the rule is simply ignored. – Keith Clarke Dec 09 '13 at 21:01
  • 1
    Sorry Keith, that just isn't the case. There is likely something else going on. See my edit above, or this link: http://jsfiddle.net/ryleyb/3J4bp/1/ – Ryley Dec 09 '13 at 22:00
  • @Riley Well, I can't argue with a working jsfiddle! ;-) Thanks for taking the time to do this. I see the {} wrapping the rule attributes, which are not present in the jQuery validate function [documentation](http://jqueryvalidation.org/reference). I didn't have these or even think to try them (I'm no JS expert). I've now moved my rules to inline with the elements and everything's working OK, but your efforts will help others. Thanks – Keith Clarke Dec 10 '13 at 11:03
0

As the rules parameter block seems to dislike elements with a name that starts with leading zeros - and I hadn't thought of wrapping the rule parameters as per @Riley's answer above - I have moved the rules inline, so this validates OK...

<input id="00ND0000005Rtcx" name="00ND0000005Rtcx" style="width: 150px;" type="text" minlength="15" maxlength="15" title="Enter your 15-character reference" required="true" />