Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys im working with my validations recently. i validate my text box to allow only numbers using this code
JavaScript
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
Updated 25-Dec-16 20:05pm

Haven't tried it but something like following should do your job-
JavaScript
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.
 
Share this answer
 
Comments
Member 12919944 26-Dec-16 2:16am    
it functions well but i cannot erase?cannot use backspace :O
If it is for onkeydown event, then it should be:
C#
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,
C#
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.
 
Share this answer
 
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 ?
onkeydown="return (event.keyCode>=65 && event.keyCode<=90 || event.keyCode==32 || event.keyCode==8 || event.keyCode==110);

Adding, Keycode "110" which is (.) for complete name.
 
Share this answer
 
Comments
CHill60 31-Mar-21 12:15pm    
A period (.) is neither a letter nor a space. It is punctuation. The OP did not want punctuation.
Stick to answering newer posts where the OP still needs help, and make sure you are answering the question actually asked

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