Question: I have a form where a portion of it is repeating. Due to that the name of the input fields are part of a group of fields. For example:

```
<input type="text" name="order[line1][address1]" id="address1_line1"> 
<input type="text" name="order[line2][address1]" id="address1_line2">

```

When using the jQuery Validation Plugin (<https://jqueryvalidation.org>) I can't find a way to set rules and messages for these fields, for example:

```
$("#registration-form").validate({
        rules: {
            order[*][address1]: "required",
        },
        messages: {
            order[*][address1]: "Please enter an address",

        }
    }); 

```

.. with \* being a wildcard.

I can add a required tag to the input like so:

```
<input type="text" name="order[line1][address1]" id="address1_line1" required>

```

... but I still can't add a custom message to the validation.

Appreciate anybody's help.

Answer: 
You cannot directly do this...

```
$("#registration-form").validate({
    rules: {
        order[*][address1]: "required",
    },
    messages: {
        order[*][address1]: "Please enter an address",

    }
});

```

...because the plugin is looking for the exact `name` of the element, not a wildcard selector. 

However, using [the `.rules('add')` method](https://jqueryvalidation.org/rules/) combined with [a "contains" selector](https://api.jquery.com/attribute-contains-selector/) and [a jQuery `.each()`](http://api.jquery.com/jquery.each/), you could do something like this, which will apply the `required` rule to all field names containing `"address"`...

```
$('[name*="address"]').each(function() {
    $(this).rules('add', {
        required: true,
        messages: {
            required: "Please enter an address"
        }
    });
});

```
You can create a variable with custom validation.

```
var ruleSet = {
    required: true,
    number: true
};

$('#myForm').validate({
    rules: {
        field1: ruleSet,
        field2: ruleSet,
        field3: ruleSet
    }
});

```