Click here to Skip to main content
13,148,145 members (36,099 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hello guys im working with my validations recently. i validate my text box to allow only numbers using this code
onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);"
how can i convert this to allow only spaces and letters only ?

What I have tried:

onkeydown = "return (!(event.keyCode>=65) && event.keyCode!=32);"
Posted 25-Dec-16 19:14pm
Updated 25-Dec-16 20:05pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Haven't tried it but something like following should do your job-
onkeydown = "return((event.keyCode >= 65 && event.keyCode <= 120) || (event.keyCode==32));"


Hope, it helps.
If it doesn't work, please let me know.

Note: Would suggest to use regex for better and easier solution.
  Permalink  
Comments
Member 12919944 26-Dec-16 2:16am
   
it functions well but i cannot erase?cannot use backspace :O
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

If it is for onkeydown event, then it should be:
return (event.keyCode>=65 && event.keyCode<=90 || event.keyCode==32);

to capture the actual keyboard key.
Check this out: Keyboard events | JavaScript Tutorial[^]
+++++[round 2]+++++
Quote:
it functions well but i cannot erase?cannot use backspace :O

Then add one more condition to check for backspace key,
return (event.keyCode>=65 && event.keyCode<=90 || event.keyCode==32 || event.keyCode==8);

Refer to this for other keycodes in JavaScript KeyboardEvent Value (keyCodes, metaKey, etc) | CSS-Tricks[^]
+++++[round 3]+++++
Quote:
hey man another question i tested now to validate and allow only numbers and colon and backspace i try this codes onkeydown = "return (!(event.keyCode>=65 && event.keyCode!=32 || event.keyCode==186 || event.keyCode==8;" but it didnt work well for me ?

I have given you enough code and reference, It is up to you to straighten up your logic. If you want to accept numbers, what is the range of keycodes for numbers? If you do not want to accept space, why bother including it then checking that it is not? Watch the syntax too.
  Permalink  
v10
Comments
Member 12919944 26-Dec-16 2:17am
   
it functions well but i cannot erase?cannot use backspace :O
Peter Leow 26-Dec-16 2:24am
   
add one more check for backspace key then,
event.keyCode==8
Added in my solution.
Member 12919944 26-Dec-16 3:41am
   
thankyou so much this is very helpful .
Peter Leow 26-Dec-16 5:46am
   
You are welcome.
Member 12919944 26-Dec-16 21:36pm
   
sir ?
Member 12919944 26-Dec-16 12:05pm
   
hey man another question i tested now to validate and allow only numbers and colon and backspace i try this codes onkeydown = "return (!(event.keyCode>=65 && event.keyCode!=32 || event.keyCode==186 || event.keyCode==8;" but it didnt work well for me ?
Peter Leow 26-Dec-16 23:30pm
   
I have given your enough code and reference. It is up to you to straighten up your logic. If you want to accept numbers, what is the range of keycodes for numbers? If you do not accept space, why bother including it then checking that it is not? Watch your syntax too.
Member 12919944 28-Dec-16 0:47am
   
please just last one example im new at javascript so its a little harder for me to get it.
Peter Leow 28-Dec-16 0:55am
   
return (event.keyCode>=48 && event.keyCode<=57 || event.keyCode==185 || event.keyCode==8);
Member 12919944 28-Dec-16 0:57am
   
can you explain me how does this code work ? i mean its logic ?
Peter Leow 28-Dec-16 1:01am
   
learn from http://www.w3schools.com/js/js_operators.asp

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170924.1 | Last Updated 26 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100