Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking the first number in a text field

I have a text input binded to a variable that contains a 5 digits number. How can I block the first digit so only the other 4 are editable?

Currently I have this: ng-model="my_var" ng-pattern="/^\d{5}$/"

Please note that the value is two-way binded, which means I'm showing it and the user can edit /save it.

like image 552
alexandernst Avatar asked Dec 19 '13 09:12

alexandernst


People also ask

How do I restrict a number in input field?

Complete HTML/CSS Course 2022 To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”

How do I restrict a number in a text box in HTML?

You can use an <input type="number" /> . This will only allow numbers to be entered into othe input box.

How do you set the value of an input type number?

The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed. min - specifies the minimum value allowed.


1 Answers

You could use a custom directive that would work in conjunction with ngModel.

It could use the parser/formatter to modify the value read/printed.

Read more about it here: http://docs.angularjs.org/api/ng.directive:ngModel.NgModelController ($formatters and $parsers)

like image 140
Neikos Avatar answered Sep 21 '22 21:09

Neikos