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.
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"/>
This seems hacky, but does the trick:
function digitKeyOnly(e) {
var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
var value = Number(e.target.value + e.key) || 0;
if ((keyCode >= 37 && keyCode <= 40) || (keyCode == 8 || keyCode == 9 || keyCode == 13) || (keyCode >= 48 && keyCode <= 57)) {
return isValidNumber(value);
}
return false;
}
function isValidNumber (number) {
return (1 <= number && number <= 10 )
}
<input class="text" name="Serial_Num" type="text" id="SrNo" size="2" maxlength="2" onkeypress="return digitKeyOnly(event)" />
Easiest would be that you turn the entry in the texbox into a number and then just:
if (1 <= number && number <= 10 ) {
true;
}
Match against /^\d+$/. $ means "end of line", so any non-digit characters after the initial run of digits will cause the match to fail.
Edit:
RobG wisely suggests the more succinct /\D/.test(z). This operation tests the inverse of what you want. It returns true if the input has any non-numeric characters.
Simply omit the negating ! and use if(/\D/.test(z)).
here is how to validate the input to only accept numbers this will accept numbers like 123123123.41212313
<input type="text"
onkeypress="if ( isNaN(this.value + String.fromCharCode(event.keyCode) )) return false;"
/>
and this will not accept entering the dot (.), so it will only accept integers
<input type="text"
onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"
/>
this way you will not permit the user to input anything but numbers
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
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();
}
});