I think the easy way to do this would be:
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
But the problem comes up when you paste some text then HTML5's number type input may be a better choice:
<input type="number" />
BTW you can find better suggestions if you search the SO like this.
jquery - Trying to get numbers from keypress document, javascript - Stack Overflow
javascript - Number validate at keypress - Stack Overflow
javascript - How do I check if the key pressed on a form field is a digit (0 - 9)? - Stack Overflow
javascript - KeyboardEvent: how to get numeric key code while 'Shift' is pressed? - Stack Overflow
I think the easy way to do this would be:
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />
But the problem comes up when you paste some text then HTML5's number type input may be a better choice:
<input type="number" />
BTW you can find better suggestions if you search the SO like this.
var input = document.getElementById('my_text');
input.onkeydown = function(e) {
var k = e.which;
if ( (k < 48 || k > 57) && (k < 96 || k > 105) && k!=8) {
e.preventDefault();
return false;
}
};
and
<input type="text" id="my_text" size="30"/>
Numbers are 48 through 57, so...
$(document).keypress(function (e) {
var key = e.keyCode || e.charCode;
if (key >= 48 && key <= 57) {
alert('You pressed ' + (key - 48));
}
});
See demo
Source: http://www.quirksmode.org/js/keys.html
Keypress events yield a keyCode of 0 in Firefox, and the ASCII character value everywhere else. Keypress events yield a charCode of the ASCII character value in Firefox. Therefore, you should use (e.keyCode || e.charCode) to get the character value.
Also note that your code also wouldn't work because alert should accept one argument. In Firefox, at least, calling alert with no arguments throws an exception.
With those two issues fixed, your code will now be:
$(document).keypress(function (e) {
if ((e.keyCode || e.charCode) == <number from 48..57 inclusive>) {
alert('something');
}
});
Example: http://jsfiddle.net/gRrk6/
The first character is unrestricted because you have nested keypress handlers. Try this:
$('.Number').keypress(function (event) {
var keycode = event.which;
if (!(event.shiftKey == false && (keycode == 46 || keycode == 8 || keycode == 37 || keycode == 39 || (keycode >= 48 && keycode <= 57)))) {
event.preventDefault();
}
});
Try
$('.Number').keyup(function (event) {
var keycode = event.which;
if (!(event.shiftKey == false && (keycode == 46 || keycode == 8 || keycode == 37 || keycode == 39 || (keycode >= 48 && keycode <= 57)))) {
event.preventDefault();
}
});
Use event.key to get the actual value. To check if integer, just use isFinite
Copyinput.addEventListener("keydown", function(event) {
const isNumber = isFinite(event.key) && event.key !== ' ';
});
Other option:
Copyconst isNumber = /^[0-9]$/i.test(event.key)
An easier HTML solution would be to use the number type input. It restricts to only numbers (kind of).
Copy<input type="number">
Either way, you should clean all user input with:
Copystring.replace(/[^0-9]/g,'');
See these:
- HTML Text Input allow only Numeric input
- allow digits only for inputs
Options:
- Uisng event.which: It returns numeric value but it is deprecated.
- Your idea of creating map is cool. To create the map, instead of using "event.key", use "event.code".
EVENT.CODE is your life saver.
A modern solution is
window.addEventListener('keydown', (e) => {
// ...
const matches = /Digit([0-9])/.exec(e.code);
if (matches) {
const digit = +matches[1];
console.log(digit);
// ...
}
});
Simple way to check that enter value is numeric is:
var checknumber = $('#textbox_id').val();
if(jQuery.isNumeric(checknumber) == false){
alert('Please enter numeric value');
$('#special_price').focus();
return;
}
Just need to apply this method in Jquery and you can validate your textbox to just accept number only.
function IsNumberKeyWithoutDecimal(element) {
var value = $(element).val();
var regExp = "^\\d+$";
return value.match(regExp);
}
Try this solution here