Reputation: 2117
How can I recognize a leading zero in an alphanumeric field using ng-pattern in angular?
<body ng-app>
<h1>Regex test</h1>
<form name="myForm">
<input name="myNumberField" ng-model="myNumber" ng-pattern="REGEX HERE" required/>
<span ng-show="myForm.myNumberField.$error.pattern">Invalid pattern</span>
</form>
</body>
I tried ng-pattern="/(^0[0-9].*$)/"
and ng-pattern="/(^0.*$)/"
Here is a plunker: http://plnkr.co/edit/laB3Q7nApuoLT9NYnCGX?p=preview
For example:
Invalid Patterns:
0AHFFB
00AJFH
0038429347
Valid Patterns:
AHFFB
AJFBN
32400342
30292900
Upvotes: 0
Views: 1146
Reputation: 221
You may use the expression:
^(?:\w|[1-9])(?:\w|\d)+$
It reads:
Test it on https://regex101.com/
Upvotes: 0