gaurav
gaurav

Reputation: 127

combine two regex for angularjs form field

How to combine this two regex for angularjs form field

\d{4}([- ]*)\d{6,7} 

OR

[a-zA-Z0-9]{4,12}

I tried like this

<input type="text" pattern="\d{4}([- ]*)\d{6,7} | [a-zA-Z0-9]{4,12}" class="form-control" ng-model="formData.username" required placeholder="username">

Upvotes: 0

Views: 928

Answers (2)

smnbbrv
smnbbrv

Reputation: 24541

The problem is that angular uses single pipe symbol for filters and you are not allowed to use it within the templates. Usual case would be a binary OR but your case may also have the problem...

So what you can do is creating a variable on a controller which holds your pattern and use the variable in the template:

<input type="text" ng-pattern="myctrl.pattern" class="form-control" ng-model="formData.username" required placeholder="username">

Upvotes: 0

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

In order to get the alternative of 2 regular expressions, you need to wrap them both in parentheses:

<input type="text" pattern="(\d{4}([- ]*)\d{6,7})|([a-zA-Z0-9]{4,12})" class="form-control" ng-model="formData.username" required placeholder="username">

Upvotes: 2

Related Questions