By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,461 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,461 IT Pros & Developers. It's quick & easy.

how to filter alphanumeric in js

P: 94
Hi all,
Do anyone know the code to filter alphanumeric characters alone during keypress event. I've tried but its allowing special characters like ! for 1 , @ for 2 etc.
My code is

Expand|Select|Wrap|Line Numbers
  1. function FilterAlphaNumeric()
  2.   { 
  3.     if((57>=event.keyCode && event.keyCode>=48) 
  4.         || (105>=event.keyCode && event.keyCode>=96) 
  5.         || (90>=event.keyCode && event.keyCode>=65) 
  6.         || (40>=event.keyCode && event.keyCode>=37)
  7.         || (event.keyCode == 8) || (event.keyCode == 46)  
  8.         || (event.keyCode == 13) || (event.keyCode == 32))
  9.     {
  10.       event.returnValue=true;
  11.     }
  12.     else
  13.     {
  14.     event.returnValue = false;
  15.     }
  16.   }
Oct 23 '07 #1
Share this Question
Share on Google+
7 Replies


Ferris
100+
P: 101
Hi

I think you need to add a shiftKey test...

[HTML]
function FilterAlphaNumeric()
{
if(event.shiftKey)
{
if(90>=event.keyCode && event.keyCode>=65) //you can press shiftkey when input alpha.
{
event.returnValue=true;
}
else
{
event.returnValue = false;
}
}
else
{
if((57>=event.keyCode && event.keyCode>=48)
|| (105>=event.keyCode && event.keyCode>=96)
|| (90>=event.keyCode && event.keyCode>=65)
|| (40>=event.keyCode && event.keyCode>=37)
|| (event.keyCode == 8) || (event.keyCode == 46)
|| (event.keyCode == 13) || (event.keyCode == 32))
{
event.returnValue=true;
}
else
{
event.returnValue = false;
}
}
}

[/HTML]
Oct 23 '07 #2

dmjpro
100+
P: 2,476
Hi all,
Do anyone know the code to filter alphanumeric characters alone during keypress event. I've tried but its allowing special characters like ! for 1 , @ for 2 etc.
My code is

Expand|Select|Wrap|Line Numbers
  1. function FilterAlphaNumeric()
  2.   { 
  3.     if((57>=event.keyCode && event.keyCode>=48) 
  4.         || (105>=event.keyCode && event.keyCode>=96) 
  5.         || (90>=event.keyCode && event.keyCode>=65) 
  6.         || (40>=event.keyCode && event.keyCode>=37)
  7.         || (event.keyCode == 8) || (event.keyCode == 46)  
  8.         || (event.keyCode == 13) || (event.keyCode == 32))
  9.     {
  10.       event.returnValue=true;
  11.     }
  12.     else
  13.     {
  14.     event.returnValue = false;
  15.     }
  16.   }
try this ...

Expand|Select|Wrap|Line Numbers
  1. if((57>=event.keyCode && event.keyCode>=48) 
  2.         || (105>=event.keyCode && event.keyCode>=96) 
  3.         || (90>=event.keyCode && event.keyCode>=65) 
  4.         || (40>=event.keyCode && event.keyCode>=37)
  5.         || (event.keyCode == 8) || (event.keyCode == 46)  
  6.         || (event.keyCode == 13) || (event.keyCode == 32))
  7.     {
  8.       //event.returnValue=true;
  9.     }
  10.     else
  11.     {
  12.     //event.returnValue = false;
  13.     event.keyCode = 0;
  14.     }
  15.  
Debasis
Oct 23 '07 #3

P: 94
try this ...

Expand|Select|Wrap|Line Numbers
  1. if((57>=event.keyCode && event.keyCode>=48) 
  2.         || (105>=event.keyCode && event.keyCode>=96) 
  3.         || (90>=event.keyCode && event.keyCode>=65) 
  4.         || (40>=event.keyCode && event.keyCode>=37)
  5.         || (event.keyCode == 8) || (event.keyCode == 46)  
  6.         || (event.keyCode == 13) || (event.keyCode == 32))
  7.     {
  8.       //event.returnValue=true;
  9.     }
  10.     else
  11.     {
  12.     //event.returnValue = false;
  13.     event.keyCode = 0;
  14.     }
  15.  
Debasis
I tried urs but cant c any difference. I am trying to find the keycodes and then restricting it.
Oct 23 '07 #4

P: 94
I tried urs but cant c any difference. I am trying to find the keycodes and then restricting it.
Expand|Select|Wrap|Line Numbers
  1. if((57>=event.keyCode && event.keyCode>=48) 
  2.         || (122>=event.keyCode && event.keyCode>=96) 
  3.         || (90>=event.keyCode && event.keyCode>=65)         
  4.         || (event.keyCode == 8) //|| (event.keyCode == 46) for dot 
  5.         || (event.keyCode == 13) || (event.keyCode == 32))
  6.     {
  7.       event.returnValue=true;
  8.     }
  9.     else
  10.     {
  11.     event.returnValue = false;
  12.  
  13.     }
I got it. I found the keycodes by pressing each key and then filtered only the required. It worked. Thanks for Ferris and Debasis for your outputs.
Oct 23 '07 #5

gits
Expert Mod 5K+
P: 5,390
hi ...

is there a very urgend reason why not checking the input after it was done (validate the input field's value after change), this would be much easier and what about different keyboard-layouts? could it be that there would be a never ending story with checking keypress during input?

kind regards
Oct 23 '07 #6

P: 94
hi ...

is there a very urgend reason why not checking the input after it was done (validate the input field's value after change), this would be much easier and what about different keyboard-layouts? could it be that there would be a never ending story with checking keypress during input?

kind regards
Hi Gits,
You answered me in many queries that i've posted.If i troubled you this time, I really regret for it. Sorry, I'll not let it happen again.
Oct 23 '07 #7

gits
Expert Mod 5K+
P: 5,390
Hi Gits,
You answered me in many queries that i've posted.If i troubled you this time, I really regret for it. Sorry, I'll not let it happen again.
hi ... no you didn't ... its just a question ... could you tell me the reason? you got it to work, so you have an answer, but i only thought that it could be better to use a simple solution since i don't know what happens to the solution with different keyboard-layouts, or may be that is not issue for you? or even an issue at all ... since i'm not sure about this? ... as i said ... it was just a thought, a question ... nothing more ;) ...

kind regards
Oct 23 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.