1

So I have an input with the type text:

    <div class="row">
        <div class="form-group">
            <input type="text" class="form-control col-sm-12" id="last-name-reservation-form" placeholder="Last name" required>
        </div>
    </div>

How would I be able to filter on the input characters, such that it only takes text and not numbers? Sort of similar to how type="number" only allows numbers to be input.

Do I have to make something in the controller or can it be done directly in the .html?

2 Answers 2

2

You can just use ng-pattern

For restricting only alphabets,

<input type="text" ng-model="myText" name="inputName" ng-pattern="/^[a-zA-Z]*$/" >

For restricting only numbers,

<input type="text"  ng-model="myText" name="inputName" ng-pattern="/^\d+$/">
3
  • Had no idea you could do it like that. Short and easy. Thanks! Commented Jan 9, 2017 at 17:12
  • I plan to, but can only do 10mins after posting the question :) Commented Jan 9, 2017 at 17:18
  • i think its almost 12 minutes :P Commented Jan 9, 2017 at 17:19
0

You could put

pattern="[A-Za-z]+"

in the input tag to make the pattern match.

2
  • Why {28} and not * / +? Commented Jan 9, 2017 at 17:15
  • I was just giving an example but thanks I changed it to +. Commented Jan 9, 2017 at 17:20

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.