Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to validate 2 digit and 2 decimal like this 10.56 , 7.55, 07.22, 8.3, 8.0 true but 10.223, 6, 8.345, false.
Posted
Comments
Kornfeld Eliyahu Peter 17-Dec-14 12:27pm    
Ever heard of regular expressions?
Maciej Los 17-Dec-14 12:48pm    
Validate... In what aspect?
Sergey Alexandrovich Kryukov 17-Dec-14 17:55pm    
Why doing so?
—SA

Your requirement is not clear enough.
Assuming your number need to fulfill the following, use a regex to check for valid entry:
1) the number starts with at least one digit
2) the number has exactly one decimal separator (the dot)
3) the number has exactly one or two fractional digits
4) the number may be surrounded by spaces
JavaScript
^\s*\d+\.\d\d?\s*$
Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Dec-14 17:56pm    
5ed.
—SA
Andreas Gieriet 17-Dec-14 18:24pm    
Thanks for your 5!
Cheers
Andi
Untested: ( x < 100 ) && ( x == ( (int) ( x * 100 ) ) / 100.0 )

(Or does it need to handle negative values?)
 
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