r3plica
r3plica

Reputation: 13367

AngularJs input with required alphanumeric

So I have this form that has an id input field. I want to restrict it to alpha numeric with no spaces so I set this up:

self.pattern = /^[a-z0-9]*$/;

Which is used in my view like this:

<input class="form-control" name="id" type="text" ng-model="controller.model.id" ng-disabled="controller.id" ng-pattern="/^[a-z0-9]*$/" ng-trim="" ng-maxlength="10" required />

But I want to modify my pattern to say that there must be at least 1 letter, but it can have 0 digits. Can someone let me know how to do this?

Upvotes: 2

Views: 493

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626801

You may use

ng-pattern="/^\d*[a-z][a-z\d]*$/"

Add i modifier if you need to also match uppercase letters:

ng-pattern="/^\d*[a-z][a-z\d]*$/i"

If you need to disallow leading/trailing whitespaces add ng-trim="false".

Pattern details

  • ^ - start of string
  • \d* - 0+ digits
  • [a-z] - a letter
  • [a-z\d]* - 0 or more letters or digits
  • $ - end of string.

Upvotes: 2

Related Questions