Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set maximum length in input type=number?

I have a text field.

<input type="number" min="0" max="999">

But i am able to type as many numbers inside the text box.

As I set max=999, I don't want that to happen. Anyone knows the solution for this? I have tried ng-maxlength and maxlength but doesn't work.

like image 346
vishnuprasad kv Avatar asked May 31 '16 06:05

vishnuprasad kv


People also ask

How do you set the maximum length of an input type number?

The <input> maxlength attribute is used to specify the maximum number of characters enters into the <input> element. Attribute Value: number: It contains single value number which allows the maximum number of character in <input> element. Its default value is 524288.

How do you set range in input type number?

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below. Tip: Always add the <label> tag for best accessibility practices!

Which specifies the maximum length of the input field?

The maxlength attribute specifies the maximum number of characters allowed in the <input> element.


2 Answers

max attribue specifies the maximum possible value that we can specify.

for more details see this link

I think the following will be helpful to you,

//maxlength="10"
<input type="number" onKeyPress="if(this.value.length==10) return false;" />
like image 168
Reshma Reghunatha Panicker Avatar answered Sep 24 '22 17:09

Reshma Reghunatha Panicker


Try

<input type="number" min="0" max="999" 
   onKeyUp="if(this.value>999){this.value='999';}else if(this.value<0){this.value='0';}"
id="yourid">
like image 25
mujuonly Avatar answered Sep 23 '22 17:09

mujuonly