If-statement triggering despite not every condition met - javascript

I've got this if condition:
(48 - 57 are the typewriter numerics, 96 - 105 apply to the numeric keypad)
if((e.ctrlKey && e.altKey) && ((e.keyCode > 47 && e.keyCode < 58)) || (e.keyCode > 95 && e.keyCode < 106)){
console.log(e.altKey);
}
This is working well if i do not use the numeric keypad but the typewriter keys instead.
However, if i press ctrl + any number on the numeric keypad, the condition will trigger and output false to the console
How can that be?

did you mean?
(e.ctrlKey && e.altKey) && ((e.keyCode > 47 && e.keyCode < 58) || (e.keyCode > 95 && e.keyCode < 106))

Can you try this :
if (
( e.ctrlKey && e.altKey )
&& (
(e.keyCode > 47 && e.keyCode < 58)
|| (e.keyCode > 95 && e.keyCode < 106)
)
)
{
console.log(e.altKey);
}
You had a ) after < 58 wich was not on good place

Related

Jquery - IF-ELSE logic doesn't work

I am trying to write a jquery function that controls user input of a texfield (for currency) based on locale. The alert did tell me that the german locale is "de". But the if-else logic does not work. The preventDefault() for the specific keycodes work just fine without the if-else. Can someone please tell me what it is that i am doing wrong here?
jQuery(function($) {
$('.currency').on('keydown', function(e) {
var locale = $('#currFormat').val();
//alert(locale);
if (locale.toLowerCase() === "de" ) {
console.log(e.keyCode);
if (e.keyCode !== 46 && e.keyCode > 31 && e.keyCode !== 96 && e.keyCode !== 97 && e.keyCode !== 98 && e.keyCode !== 99 && e.keyCode !== 100 && e.keyCode !== 101 && e.keyCode !== 102 && e.keyCode !== 103 && e.keyCode !== 104 && e.keyCode !== 105 && e.keyCode !== 188 e.keyCode !== 37 && e.keyCode !==39 && (e.keyCode < 48 || e.keyCode > 57)) {
//stop all non-numbers
e.preventDefault();
} else {
// Here goes logic for not allowing comma and others (,)
}
});
});
The 'amount' value comes from the JSP.
<input type="hidden" id="CurrFormat" name="currFormat"
value="${pageContext.request.locale.language}" />
You have a typo. So technically you are selecting the element that is not present on the page.
$('#currFormat').val();
is supposed to be
$('#CurrFormat').val();
Also is this a copy paste error as well
e.keyCode !== 188 e.keyCode !== 37 --> missing logical && between expressions
Also the reason it is easy to make a syntax error is because the code is not readable at all. Instead you can condense it and make it simpler.
var keyCode = e.keyCode;
var invalidKeyCodes = [37, 39, 46, 96, 97, 98, 99, 100, 101. 102, 103, 104, 105, 188];
if (invalidKeyCodes.indexOf(keyCode) < 0 && keyCode > 31 && (keyCode < 48 || keyCode > 57)) {

Here is some JavaScript that is supposed to run on the keyup event, but only if the keycode is between 48 and 57. Here is the code [duplicate]

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">

CheckNumeric value in JavaScript

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)

event.keycode issue

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.

jQuery/JS Keycodes: Several keycodes in one IF statement

I'm trying to set up some keycodes for an app I'm working on but it really drives me crazy.
I'm trying to set up keycodes which would say, from keyCode 65 to 91 , 44 , 47 and few others, do function.
So I have this:
var e = event || evt;
if ((e.keyCode > 65 || e.Keycode < 91)){
// Do function
}
which works find. Now if I try to add another keycode it doesn't work.
This is what I tried:
if ((e.keyCode > 65 || e.Keycode < 91) && (e.keyCode == 44) && (e.keyCode == 47)){
Does someone help me to add different keycodes in one If statement?
Thanks alot
Try this
if (
// get all the keys between 65-90
(e.keyCode >= 65 && e.keyCode <= 90)
// or 44 or 47
|| e.keyCode == 44 || e.keyCode == 47)
{
// do stuff
}
If the conditional logic is tripping you up, I think you might be best served by thinking about the numbers you want to include (not exclude). Break them into ranges and put each range on its own line. Start with pseudo code:
if
// keys between 31-47
// or keys between 58-90
then
// do stuff
end
Then fill in the conditions:
if (
// keys between 31-47
(e.keyCode >= 31 && e.keyCode <= 47)
// or keys between 58-90
|| (e.keyCode >= 58 && e.keyCode <= 90)
)
{
// do stuff
}
If you want everything from 31 to 90 except for those from 48 through 57:
if (e.keyCode >= 31 && e.keyCode <= 90 && !(e.keyCode >= 48 && e.keyCode <= 57)) {
// whatever
}
Now that could be written equivalently as:
if (e.keyCode >= 31 && e.keyCode <= 47 || e.keyCode >= 58 && e.keyCode <= 90) {
// whatever
}
if ((e.keyCode > 65 && e.Keycode < 91) || e.keyCode == 44 || e.keyCode == 47){
//do stuff
}

Categories

Resources