Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found this code but am having trouble getting it to accpet . and , aswell.

this is the general code :
let check = (evt.which) ? evt.which : evt.keyCode
if (check > 31 && (check < 48 || check > 57 ))
return false;
return true;

Have I just understood it wrong?

What I have tried:

I have tried:
if (check > 31 && (check < 48 || check > 57 ) || check == 44 || check == 46)

if (check > 31 && (check < 48 || check > 57|| check == 44 || check == 46))
Posted
Updated 21-Nov-22 1:24am

I would write it this way
JavaScript
let check = (evt.which) ? evt.which : evt.keyCode
if ((check <= 31) || (check >= 48 && check <= 57 ) || (check === 44) || (check === 46))
  return true; // whitelist
return false;
 
Share this answer
 
v2
You should not use that function, see KeyboardEvent.keyCode - Web APIs | MDN[^]. Also what are those values supposed to represent?
 
Share this answer
 
The ASCII code you want are 46 ('.') and 44 (',') so that code should work - I'd suspect that it's the line above or the event you are handling that is causing the problem.

Break out the Javascript debugger (Debug JavaScript - Chrome Developers[^] for Chrome) and start looking at exactly what is in variables while your code executes.
 
Share this answer
 
v2
Comments
CPallini 21-Nov-22 7:35am    
Note the if is intended to 'filter-out', rather than 'filter-in', hence OP code should NOT work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900