Lightning – Using Multiple Patterns with lightning:input (text)

I have a field that checks for 2 patterns. What I currently have is, in the text field's onchange handler, i check if the inputted string matches a certain pattern. However, I want to use lightning's OOTB feature that colors the field red and shows a label below the field if a pattern is invalid. I can do just that for 1 pattern:

<lightning:input aura:id="myField" label="test field" value="{!v.valHolder}" onchange="{!c.txt_change_testHandler}" pattern="\d{8}" />

The above snippet works. As you can see, it's just a simple pattern. But I currently have 2 patterns to check. The 2nd pattern looks something like this:

[a-zA-Z0-9]{12}-\d{3}

My problem now is, how do I check for these 2 patterns via the lightning input's pattern attribute, and not use the onchange handler at all?

Best Answer

Use the "OR" operator (|):

pattern="(\d{8}|[a-zA-Z0-9]{12}-\d{3})"
Related Topic