Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am able to validate the numbers only now . but i cannot continue to allow the period sign also. please help.

What I have tried:

onkeydown="return (!(event.keyCode>=65) && event.keyCode=32);
Posted
Updated 4-Feb-17 18:54pm
Comments
Member 12919944 4-Feb-17 14:36pm    
?
[no name] 4-Feb-17 16:01pm    
Okay so put the code in for the period.

check this
JSFiddle[^]
 
Share this answer
 
Comments
Peter Leow 5-Feb-17 5:50am    
Good effort. 5ed!
Karthik_Mahalingam 5-Feb-17 5:52am    
Thanks Peter
Member 12919944 5-Feb-17 9:11am    
this was good :D
There are several issues with the code.

1. The keyCode 32 is for space. The period sign is either 110 or 190. You can reference to JavaScript Event KeyCodes[^] to find out the correct key code for each keyboard character.

2. For comparison operator use == , = is for assignment, you can learn more from JavaScript Operators[^]

3. The logical operator is wrong (&&), (Logical AND) Returns true if both logical operands are true. Otherwise, returns false.

4. You might need to add extra logic to prevent user from entering many periods, like 1...2

JavaScript
<input id="test" type="text" 
    onkeydown="return (!(event.keyCode >= 65) || event.keyCode == 110 || event.keyCode == 190);" />
 
Share this answer
 

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