473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to filter alphanumeric in js

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
7 11841
Ferris
101 100+
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
2,476 2GB
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
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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

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

Similar topics

3
by: Daniel Tonks | last post by:
OK, here's possibly a weird one. Is there any way to do string comparisons and ignore all non-alphanumeric characters? For instance, check "foobar" and have it match an existing record of "f$#!oo...
1
by: Avnish | last post by:
Hi, I am looking for some form of validation for all the alphanumeric characters in the entire unicode range e.g. the validation should also accept japanese characters but should restrict...
4
by: Matt | last post by:
I want the javascript to test an alphanumeric (a string contains alphabet or numbers only) string. Should I write a regular expression? What's the best way to do? please help. thanks
6
by: ironcito | last post by:
Hello! I'm looking for a way to have a field in my database that will automatically be filled with a random 4-character alphanumeric string every time I enter a new record. Like an autonumber...
9
by: fooboo | last post by:
Does anyone know if a easier way (built in function, or something) that can verify that a string is an alphanumeric number? Here is what I am doing now: for(i=0; i < strlen(temp); i++){...
7
by: Fernando Rodríguez | last post by:
Hi, How can I know if a string only has alfanumeric chars? Thanks
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
7
by: kanepart2 | last post by:
Hey all, I have to validate a textbox in windows forms for alphanumeric characters such that non alphanumeric key presses are ignored. Some help would be appreciated
1
by: chris fellows | last post by:
I have a C# webservice with a web method that accepts a string parameter that contains encrypted data. For some parameter values with non-alphanumeric values then the web method fails with an HTTP...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.