Detect numbers or letters using jquery / javascript?

I want to use the if statement to run the code only if the user enters a letter or number.

I could use

if(event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50..) { // run code } 

Is there an easier way to do this? Maybe some key codes do not work in all web browsers?

+56
javascript jquery
Feb 13 '10 at 9:26 a.m.
source share
11 answers

If you want to check the range of letters, you can use more and less:

 if (event.keyCode >= 48 && event.keyCode <= 57) alert("input was 0-9"); if (event.keyCode >= 65 && event.keyCode <= 90) alert("input was az"); 

For a more dynamic check, use a regex:

 var inp = String.fromCharCode(event.keyCode); if (/[a-zA-Z0-9-_ ]/.test(inp)) alert("input was a letter, number, hyphen, underscore or space"); 

See the MDC documentation for the keyCode property, which explains the difference between this and the which property and what events they apply to.

+122
Feb 13 2018-10-10T00
source share

Firstly, if you do this, make sure that this is a keypress event, which is the only event for which you can reliably receive information about the character that the user typed. Then I would use the approach suggested by Andy E:

 document.onkeypress = function(evt) { evt = evt || window.event; var charCode = evt.which || evt.keyCode; var charStr = String.fromCharCode(charCode); if (/[a-z0-9]/i.test(charStr)) { alert("Letter or number typed"); } }; 

If you want to test the backspace, I would use the keydown event keydown and check the keyCode of 8, because several browsers (including Chrome) do not fire the keypress event for the keypress keyword.

+21
Feb 13 '10 at 13:21
source share

Use event.key and modern JS!

No numeric codes anymore. You can check the key directly.

 const key = event.key.toLowerCase(); if (key.length !== 1) { return; } const isLetter = (key >= "a" && key <= "z"); const isNumber = (key >= "0" && key <= "9"); if (isLetter || isNumber) { // Do something } 

You can also use a simple regular expression. ^$ provides 1 character, i ignore case

 /^[a-z0-9]$/i.test(event.key) 

or individually:

 const isLetter = /^[az]$/i.test(event.key) const isNumber = /^[0-9]$/i.test(event.key) 
+14
Feb 18 '18 at 19:48
source share
 if(event.keyCode >= 48 && event.keyCode <= 90) { //the key pressed was alphanumeric } 
+7
Feb 13 '10 at 9:33
source share

checking the numbers works great for me

 $(document).ready(function () { $(".TxtPhone").keypress(function (e) { var key = e.charCode || e.keyCode || 0; // only numbers if (key < 48 || key > 58) { return false; } }); }); 
0
Apr 26 '16 at 10:59
source share

You can also use charCode with onKeyPress event:

 if (event.charCode > 57 || event.charCode < 48) { itsNotANumber(); } else { itsANumber(); } 
0
Mar 10 '17 at 12:00
source share

use $.isNumeric(value); Return Type - boolean

0
Jul 18 '17 at 8:48
source share

Accept numbers or letters using javascript using a dynamic process using a regular expression.

Add onkeypress event for specific control

onkeypress = "javascript: return isNumber (event)"

  function numOnly(evt) { evt = evt || window.event; var charCode = evt.which || evt.keyCode; var charStr = String.fromCharCode(charCode); if (/[0-9]/i.test(charStr)) { return true; } else return false; } function Alphanum(evt) { evt = evt || window.event; var charCode = evt.which || evt.keyCode; var charStr = String.fromCharCode(charCode); if (/[a-z0-9]/i.test(charStr)) { return true; } else return false; } 
0
Oct 12 '17 at 7:08 on
source share

 $('#phone').on('keydown', function(e) { let key = e.charCode || e.keyCode || 0; //32 = space - border of visible and non visible characters - allows us to backspace and use arrows etc //127 - delete if (key > 32 && (key < 48 || key > 58) && key !== 127) { e.preventDefault(); return false; } }); 

modified answer @ user4584103, allows us to delete characters and move into the input field and filter out each character not num

0
Nov 07 '17 at 23:47 on
source share

As @Gibolt said, you should use event.key

Because charCode, keyCode and Which are deprecated.

0
Jun 18 '18 at 16:53
source share

For numerical values:

 function ValidNumeric() { var charCode = (event.which) ? event.which:event.KeyCode; if (charCode>=48 && charCode<=57) { return true; } else return false; } 

Here 48 and 57 are a range of numerical values.

For the alphabet:

 function ValidAplpha() { var charCode = (event.which) ? event.which:event.KeyCode; if(charCode >= 65 && charCode <= 90 || charCode>=97 && charCode<=122) { return true; } else return false; } 

Here from 65 to 90 is a range for capital alphabets (AZ) and from 97 to 122 is a range for small alphabets (az)

0
May 6 '19 at 20:24
source share



All Articles