Miquel Coll
Miquel Coll

Reputation: 771

Input pattern validation - More than one pattern

I've been using the input type inside a form in HTML and using the pattern attribute in order to better validate the data entry.

Is there a way to permit more than one pattern in a single input type?

For instance, I'd like to permit both [a-zA-Z]{1}\d{8} and d{8}\[a-zA-Z]{1}

This would be one of the inputs I've used so far but as you can see I have there just one pattern:

<input type="text" id="txtID" pattern="[a-zA-Z]{1}\d{8}" required/>

It might be possible that since my knowledge of RegEx is limited there might be a way to use a RegEx formula that permits boths patterns but I really don't know.

Upvotes: 0

Views: 1807

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You can use alternation operator | to allow alternative patterns:

[a-zA-Z]\d{8}|d{8}[a-zA-Z]

Note that you do not need to add any anchors or grouping, see pattern documentation:

This implies that the regular expression language used for this attribute is the same as that used in JavaScript, except that the pattern attribute is matched against the entire value, not just any subset (somewhat as if it implied a ^(?: at the start of the pattern and a )$ at the end).

Upvotes: 2

Related Questions