Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only allow alphanumeric using ng-pattern-restrict for first character only?

Tags:

angularjs

I'm using https://github.com/AlphaGit/ng-pattern-restrict to restrict input. What I"m trying to do is only allow alphanumeric for the first character ONLY. The problem is if I use :

ng-pattern-restrict="^[a-zA-Z0-9]+"

it works, but won't allow user to clear the whole string, which I would also like as an option.

like image 589
KingKongFrog Avatar asked Jul 29 '16 21:07

KingKongFrog


2 Answers

Update your regex to check for the empty string scenario. Example below:

^$|^[A-Za-z0-9]+

Plunker

like image 172
ScottL Avatar answered Oct 14 '22 00:10

ScottL


ng-pattern-restrict="^([A-Za-z0-9]{0,})$"

This will allow only alhpanumeric values.

like image 21
phantom Avatar answered Oct 13 '22 23:10

phantom