3

I'm currently using the jquery validate rules plugin and I'm running into the following issue. While some of my input fields have static IDs. There are some fields that are dynamically generated so the ID isn't static. Unfortunately I don't have control of the ID generated. I can use a .each loop to get the IDs, but how could I pass the ID variable to the rules to validate. Is there a way to use a variable in the field name? If these fields where just required I would use a different method, however I need to do regex checks against the input.

$j('form').validate({
    rules:{
        txtFirstName: 'required',                                                        
        txtLastName: 'required',                               
        txtEmail: 'required',
        randomIdFieldVariable: 'Regex check"
    }
);
1
  • This plugin does not use the ID of the element... it can only use the name. But no, you cannot use a variable in place of the field name within the .validate() method. You'll have to explain this better by constructing a more complete example if you want some suggested workarounds. Commented May 22, 2015 at 0:15

1 Answer 1

6

You cannot use the element id, nor a variable in place of a field name within the .validate() method.

$('#myform').validate({
    rules: {
        fieldname: { // <- MUST be the 'name' attribute of the element
            required: true
        }
    }
});

However, when the name of the element is unknown, there are other methods for adding rules.

  1. Certain rules that can be declared via a boolean value can be declared as a class.

    <input type="text" name="foo" class="required" />
    
  2. Certain rules that also exist as HTML 5 attributes can be declared simply by including the HTML 5 attribute on the input element.

    <input type="text" name="foo" required="required" />
    

    This could work for you since there is an HTML 5 validation attribute called pattern that the jQuery Validation plugin will use. The value of pattern is a regular expression. See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input

  3. You can attach the jQuery Validate plugin' .rules('add') method to a jQuery .each() using any jQuery selector you wish. You don't have to know the name in advance, but all elements for validation must still contain a unique name attribute.

    $('.myClass').each(function() {
        $(this).rules('add', {
            required: true,
            messages: { // <- optional
                required: "optional custom message"
            }
        });
    });
    
Sign up to request clarification or add additional context in comments.

5 Comments

you are the man! I didn't even think about using pattern attribute. Works like a charm!
I guess it's implicit in this example but: be sure to call .validate(...) before playing with individual field using .rules(...). It got me with complaining about settings not being available.
Thanks. It was easy to overlook this in the docs but its good to have a direct link. I did not get that deep into this and I'm missing a full example on the jqueryvalidation.org website. Maybe I should offer to write one ;-)
@gkephorus, well, there is the jquery-validate wiki page on the tag and something like several thousand code examples of it on SO.

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.