2

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?

2
  • 1
    Try ^[a-z0-9]*[a-z][a-z0-9]*$ Commented Jul 5, 2019 at 14:32
  • 1
    Use ng-pattern="/^\d*[a-z][a-z\d]*$/", add i modifier if you need to also match uppercase letters Commented Jul 5, 2019 at 14:34

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.