gaurav gaurav - 6 months ago 31
Javascript Question

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">

Answer

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">
Comments