lmcphers
lmcphers

Reputation: 468

Angularjs ng-pattern isn't working for me

I have been trying to find an answer on StackOverflow for my problem, but nothing seems to fix it.

Here is what I have:

<label>Portal Code</label>
<input type="text" name="portalcode" class="form-control" ng-model="dropoff.tfWeb" id="tfWeb" maxlength="8" ng-pattern="/^[a-zA-Z0-9]+$/" ng-disabled="report != null">
<pre>Errors: {{portalcode.value.$error}}</pre>

I am trying to add an ng-pattern directive that ensures only alphanumerics can be entered into the input. I have tried not using double quotes, I have tried using the \w shortform for alphanumerics. I've tried using a * instead of +. I'm at a loss now. No matter what I've changed it to, though, my {{portalcode.value.$error}} does not print out an error. Any help would be appreciated.

Upvotes: 1

Views: 447

Answers (2)

Esko
Esko

Reputation: 681

Try:
ng-pattern="/^[a-zA-Z0-9]*$/"

Here is the example in plunker:
http://embed.plnkr.co/UeCrkhQ6tBEj60MbsKXx/preview

Upvotes: 0

Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

It works if you wrap it in a form and use the form to access the portalcode: http://jsfiddle.net/xtfcg447/1/

Upvotes: 3

Related Questions