Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from typing in input at max value

I'd like the user to be blocked from typing more if the value is over 100. So far I have the following from reading different posts:

$('.equipCatValidation').keyup(function(e){
        if ($(this).val() > 100) {               
           e.preventDefault();                
        }
    });

To confirm I want the value not the string length, and it can't be above 100.

However this is not preventing further input in the field. What am I missing.

like image 339
Anthony Avatar asked Jun 17 '14 18:06

Anthony


People also ask

How do you prevent a user from typing in an input field?

just use onkeydown="return false" to the control tag like shown below, it will not accept values from user. Show activity on this post. One option is to bind a handler to the input event.

How do you set the maximum value of a input type number?

The max attribute specifies the maximum value for an <input> element. Tip: Use the max attribute together with the min attribute to create a range of legal values. Note: The max and min attributes works with the following input types: number, range, date, datetime-local, month, time and week.

How do you set the maximum and minimum value of an input type text?

addEventListener('change', function(e) { var num = parseInt(this. value, 10), min = 0, max = 100; if (isNaN(num)) { this. value = ""; return; } this.


2 Answers

Checking keyup is too late, the event of adding the character has already happened. You need to use keydown. But you also want to make sure the value isn't > 100 so you do need to also use keyup to allow js to check the value then too.

You also have to allow people to delete the value, otherwise, once it's > 100 nothing can be changed.

<input class="equipCatValidation" type="number" />

When using input type="number", change also needs to be on the event list.

$('.equipCatValidation').on('keydown keyup change', function(e){
    if ($(this).val() > 100 
        && e.keyCode !== 46 // keycode for delete
        && e.keyCode !== 8 // keycode for backspace
       ) {
       e.preventDefault();
       $(this).val(100);
    }
});

http://jsfiddle.net/c8Lsvzdk/

like image 63
Popnoodles Avatar answered Sep 19 '22 02:09

Popnoodles


Basically keypress events are fired before accepting the current value. So when you press on any key, keypress event is subscribed but you don't get the updated value/result for the recently pressed key. So, to get the last pressed key we can use the fromCharCode method and concat it with the value we got from the textbox. That's it,

HTML :

<input id="inputBox" type="text" />

jQuery :

$("#inputBox").on("keypress", function(e){
    var currentValue = String.fromCharCode(e.which);
    var finalValue = $(this).val() + currentValue;
    if(finalValue > 100){
        e.preventDefault();
    }
});

jsFiddle

like image 43
Md Ashaduzzaman Avatar answered Sep 17 '22 02:09

Md Ashaduzzaman