I have this function for checking text box value for entering only integer values.
function CheckNumeric(e) {
if (window.event) // IE
{
if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
event.returnValue = false;
return false;
}
}
else { // Fire Fox
if ((e.which < 48 || e.which > 57) & e.which != 8) {
e.preventDefault();
return false;
}
}
}
In this function textbox can accept 0 for value and reject any character.
Now I want to change this function to reject 0 if it is entered as the first character.
For example reject this: 010 and accept this: 10
you need to check if (e.keyCode == 48 && input.value.length == 0) then return false;
input is your text box element.
something like this
if (window.event) // IE
{
if (((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) || (e.keyCode == 48 && input.value.length == 0)) {
event.returnValue = false;
return false;
}
}
and it should be
(e.keyCode < 48 || e.keyCode > 57) && e.keyCode != 8)
instead of
(e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8)
Related
This question already has answers here:
keyCode values for numeric keypad?
(13 answers)
Closed 1 year ago.
$('#cnic1').keydown(function(){
if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 ||
event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true) )
return;
if((event.keyCode < 48 || event.keyCode > 57))//0-9
event.preventDefault();
var length = $(this).val().length;
if(length == 5 || length == 13)
$(this).val($(this).val()+'-');
});
My problem is that this code only runs in response to the numbers entered at the top of the keyboard, but does not run in response to numbers entered from the numeric keypad.
I'm thinking the answer must be that the numeric keypad has different keyCode values, but how do I find out what those are?
The numeric keypad does have different keyCode values. The following link gives a full table of keyCodes for your reference:
https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Use this code and press the keys. keycodes will be on console log;
window.onload = function(){
addEventListener("keydown",function(e){
console.log(e.keyCode);
});
}
Change if condition as
if((event.keyCode < 48 || event.keyCode > 57)&&(event.keyCode < 96 || event.keyCode > 105))//0-9
$('#cnic1').keydown(function() {
if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 ||
event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true))
return;
if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) //0-9
event.preventDefault();
var length = $(this).val().length;
if (length == 5 || length == 13)
$(this).val($(this).val() + '-');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="cnic1">
I am trying to restrict text box to accepts only decimals,integers and also - (negative sign) as well as backspace I tried with below script but its not allowing - sign
$(document).ready(function() {
$('#MainContent_txtAmount').keypress(function(event) {
if ((event.which != 46 || $(this).val().indexOf('.') != -1) &&
((event.which < 48 || event.which > 57) &&
(event.which != 0 && event.which != 8))) {
event.preventDefault();
}
var text = $(this).val();
if ((text.indexOf('.') != -1) &&
(text.substring(text.indexOf('.')).length > 2) &&
(event.which != 0 && event.which != 8) &&
($(this)[0].selectionStart >= text.length - 2)) {
event.preventDefault();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="MainContent_txtAmount" type="text" />
How can I accepts negative numbers too.
This allows the "-" sign too. Added && event.which != 45 . DEMO FIDDLE
$(document).ready(function () {
$('#MainContent_txtAmount').keypress(function (event) {
if ((event.which != 46 || $(this).val().indexOf('.') != -1) &&
((event.which < 48 || event.which > 57) &&
(event.which != 0 && event.which != 8)) && event.which != 45) {
event.preventDefault();
}
var text = $(this).val();
if ((text.indexOf('.') != -1) &&
(text.substring(text.indexOf('.')).length > 2) &&
(event.which != 0 && event.which != 8) &&
($(this)[0].selectionStart >= text.length - 2)) {
event.preventDefault();
}
});
});
Find the attached fiddle
sql fiddle demo
"-" has a charCode of 45.
change your condition event.which < 48 || event.which > 57 to
event.which != 45 && (event.which < 48 || event.which > 57)
A long time ago if found some code (on this site) that stopped the user from entering Letters into a text box, and it worked. Since finding this code I have changed it and added and now it doesn't work. This is what code I have:
var count = 1
function isNumberKey(event) {
var keyCode = window.event ? event.keyCode : event.which;
if (event.keyCode === 8 // backspace
|| event.keyCode === 46 // delete
|| event.keyCode === 13 // enter key
|| event.keyCode === 9 // tab
|| event.keyCode === 116 // F5 (refresh)
|| event.keyCode === 112 // F1
|| event.keyCode === 113 //F2
|| event.keyCode === 114 //F3
|| event.keyCode === 115 //F4
|| event.keyCode === 117 //F6
|| event.keyCode === 118 //F7
|| event.keyCode === 119 //F8
|| event.keyCode === 120 //F9
|| event.keyCode === 121 //F10
|| event.keyCode === 122 //F11
|| event.keyCode === 123 //F12
) {
return true;
}
else if ( key < 48 || key > 57) {
if (count < 6) {
count++; //adds one to count
}
else {
alert("Please Only Enter Numerical Values");
count = 1;
}
return false;
}
else return true;
}
I'm not claiming I made/wrote this code but can anyone see any problems with the code?
This will work. It was not working because you use key < 48 || key > 57 instead event.keyCode < 48 || event.keyCode > 57
var count = 1
function isNumberKey(event) {
var keyCode = window.event ? event.keyCode : event.which;
if (event.keyCode === 8 // backspace
|| event.keyCode === 46 // delete
|| event.keyCode === 13 // enter key
|| event.keyCode === 9 // tab
|| event.keyCode === 116 // F5 (refresh)
|| event.keyCode === 112 // F1
|| event.keyCode === 113 //F2
|| event.keyCode === 114 //F3
|| event.keyCode === 115 //F4
|| event.keyCode === 117 //F6
|| event.keyCode === 118 //F7
|| event.keyCode === 119 //F8
|| event.keyCode === 120 //F9
|| event.keyCode === 121 //F10
|| event.keyCode === 122 //F11
|| event.keyCode === 123 //F12
) {
return true;
}
else if ( event.keyCode < 48 || event.keyCode > 57) {
if (count < 6) {
count++; //adds one to count
}
else {
alert("Please Only Enter Numerical Values");
count = 1;
}
return false;
}
else return true;
}
this should do it:
textBox.onkeypress = function(e) {
e = e || window.event;
var charCode = (typeof e.which == "undefined") ? e.keyCode : e.which;
var charStr = String.fromCharCode(charCode);
if (/\d/.test(charStr)) {
return false;
}
};
This came up when I was asking, so it might be worth adding here that keyCode and charCode is deprecated, as at this time, so the accepted answer might not be the best.
A better way of adding the check or implementing the isNumberKey function could be:
const isNumberKey = (event: KeyboardEvent) =>
((event.key.length > 1) || event.key.match(/^\d|-$/))
This way, we check allow special keys do their function, while making sure only numbers are allowed when single character keys are pressed.
In the below code i have 2 js function for accepting alphabets and alphanumeric in which when i tested in mozilla firefox the tab is not workingfor alphanumeric and tab ,backspace ,delete is not working for alphabets pls anyone help me to solve the issue.
function alphanumeric(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 ||k == 9|| k == 32 || (k >= 48 && k <= 57));
}
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode > 64 && charCode < 91) || (charCode == 9 && charCode == 8)|| (charCode > 96 && charCode < 123))
return true;
else
return false;
}
catch (err) {
alert(err.Description);
}
}
This works in both FF and Chrome:
function alphanumeric(e) {
var k;
k = e.keyCode || e.charCode;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 9 || k == 32 || (k >= 48 && k <= 57));
}
function onlyAlphabets(e, t) {
try {
if (window.event) {
var charCode = window.event.keyCode;
} else if (e) {
var charCode = e.keyCode || e.charCode;
} else {
return true;
}
if ((charCode > 64 && charCode < 91) || (charCode == 9 || charCode == 8) || (charCode > 96 && charCode < 123)) return true;
else return false;
} catch (err) {
alert(err.Description);
}
}
In onlyAlphabetic()
(charCode == 9 && charCode == 8)
should be:
(charCode == 9 || charCode == 8)
It's not possible for charCode to be equal to both of them at the same time.
In the keypress event, some keys have keyCode == 0 so it's necessary to use charCode.
I suggest you read up on the difference between keypress and keydown/keyup, and charCode versus keyCode.
DEMO
i have a javascript code like below,
if (event.keycode != 37 && event.keycode != 39)
{
var phoneNumber = $('#phoneNumber').val();
if (phoneNumber.length < 1 && event.keyCode != 48)
$('#phoneNumber').val(0)
else if ((phoneNumber.length < 2 && event.keyCode == 48) )
event.preventDefault();
else
$('#phoneNumber').val(phoneNumber)
}
keycode 37 = left arrow, keycode 39 = right arrow but when i pressed these keys on keyboard condition which is between if block being executed, i am using chrome browser, also i used the if statement below,
if (event.keycode != 37 || event.keycode != 39)
{
var phoneNumber = $('#phoneNumber').val();
if (phoneNumber.length < 1 && event.keyCode != 48)
$('#phoneNumber').val(0)
else if ((phoneNumber.length < 2 && event.keyCode == 48) )
event.preventDefault();
else
$('#phoneNumber').val(phoneNumber)
}
urgent helps greatly appreciated,
Thanks everybody.
One issue is capitalization: it's keyCode, not keycode. Your code is using them inconsistently.