Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I want to write regex to validate textbox for decimal value (16,9).

I tried following regex:
/^(\d{1,16}\.?\d{1,9}|\{1,16})$/


It handles values with 1-16.1-9 digits.

Problem is when I just enter value like 12345678901234567 without decimal, it is not validating. It should actually invalidate, since it is taking 17 digit.

Any idea what is going wrong?

Thanks in advance
Posted
Comments
jaket-cp 8-Jan-15 5:16am    
try removing the ?
dhage.prashant01 8-Jan-15 5:26am    
My requirement is value can be int or decimal.
If I remove '?', then the it invalidates the int value.
jaket-cp 8-Jan-15 5:31am    
At a glance I read your pattern as being either the decimal number or integer number.
Didn't test or anything :)

Praveen Kumar Upadhyay 8-Jan-15 5:28am    
How this will validate, you regex is simply saying that before decimal it should be 16 character only.
dhage.prashant01 8-Jan-15 5:30am    
My requirement is

1234567890123456 - valid
12345678901234567- invalid

1.123456789 - valid
1.1234567890 - invalid

12345678901234567.1 - invalid

1 solution

Try:
^\d{1,16}(\.\d{1,9})?$
 
Share this answer
 
Comments
dhage.prashant01 8-Jan-15 5:44am    
It worked, TQ :)
Now the problem is value can be 0000000000.00
How can we handle below
0.0 or 1.0 - Valid
01.0 - Invalid
OriginalGriff 8-Jan-15 5:58am    
You could modify it to have a non-leading zero, but it gets messy:
^(0|[1-9]\d{0,15})(\.\d{1,9})?$

To be honest, I'd probably do this by parsing the data to a number and checking that if this gets much more complex.
CPallini 8-Jan-15 5:50am    
5.

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