Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  3] [ 2]  / answers: 1 / hits: 22091  / 11 Years ago, sun, april 14, 2013, 12:00:00

I have this input field:



<input type=text/>


How can I allow entering only a number that is not greater than some predefined value, like for example 10, so every attempt to enter a number greater than 10 won't be allowed?


More From » html

 Answers
44

Javascript



function createValidator(element) {
return function() {
var min = parseInt(element.getAttribute(min)) || 0;
var max = parseInt(element.getAttribute(max)) || 0;

var value = parseInt(element.value) || min;
element.value = value; // make sure we got an int

if (value < min) element.value = min;
if (value > max) element.value = max;
}
}

var elm = document.body.querySelector(input[type=number]);
elm.onkeyup = createValidator(elm);


HTML



<input type=number min=0 max=10></input>


I haven't tested it, but I think it should work.


[#78926] Friday, April 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;