I am using a text box. It should restrict all other keys except 1-99 while entering into the textbox. i need code to restrict them.
Asked
Active
Viewed 251 times
0
-
On SO it's appreciated when you make an effort at solving your issue. What have you tried? Please show the current code you're using and what specifically it isn't doing. – DACrosby Aug 26 '15 at 04:35
2 Answers
1
<input type="number" onkeydown="limit(this);" onkeyup="limit(this);">
LIMIT With JS
function limit(element)
{
var max_chars = 2;
if(element.value.length > max_chars) {
element.value = element.value.substr(0, max_chars);
}
}

Ghulam Ali
- 357
- 4
- 17
-
i want to restrict the other keys like 123, 786,etc. the third digit should also be restricted. – harika mandula Aug 25 '15 at 10:36
-
yes, with this way you can validate with regular expression var regular /^[0-9]{1,99}$/ – Ghulam Ali Aug 25 '15 at 11:11
0
The answers provided for this SO question should help you figure it out. It's a combination of HTML and JavaScript. There are two main ideas: 1 - use input with type=number as Ghulam Ali suggested to restrict inputs to your range of values and augment that with some JS to restrict the length. 2 - use input with type=text to restrict the length and augment it with some JS to restrict inputs to your range of values.

Community
- 1
- 1

Dan Iveson
- 926
- 6
- 10