KingKongFrog
KingKongFrog

Reputation: 14419

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

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.

Upvotes: 8

Views: 30157

Answers (4)

phantom
phantom

Reputation: 1064

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

This will allow only alhpanumeric values.

Upvotes: 1

Soumya
Soumya

Reputation: 1

This expression will help in clearing the input as well not allow any spcial character in the input at any position : ng-pattern-restrict="^$|^[A-Za-z0-9]+$"

Upvotes: 0

Raj
Raj

Reputation: 489

This pattern allows at least one alphabets and one numeric value but does not allows any special characters.

ng-pattern="/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/"

Upvotes: 0

ScottL
ScottL

Reputation: 1755

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

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

Plunker

Upvotes: 7

Related Questions