Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am using a Regular expression to validate a numeric field in my C#.net application.

I want to accept decimal data in the format 14.2 (NNNNNNNNNNNNNN.NN), for which I am using "\d{0,14}[.]{0,1}\d{0,2}". The problem is this expression is matching negative values also.

Please suggest a solution.

Thanks in advance.

Gitika
Posted
Comments
valza 16-Jul-12 3:45am    
Before regEx. Check if is not less then 0 :)
if( x <= 0)
return;
gitika.khurana 16-Jul-12 4:05am    
The parameter is a String. And I wish to use RegEx only to do all the pattern matching.

Try adding '^' to the front (and '$' to the end) - it specifies that the digits must be at the start of the string. You also need to escape the '.' in the "[.]" as otherwise it will match any character
Try:
^\d{0,14}(\.\d{0,2}){0,1}$


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
gitika.khurana 16-Jul-12 4:25am    
Your solution worked fine. Thanks.
But I would like to mention here, that '.' in the "[.]" matches only the . character, and not any character.

That functionality is achieved when we do not use the square brackets.
Use a ^ for indicating that the match has to be found at the start of the string, and a $ that no other characters must follow:
"^\d{0,14}[.]{0,1}\d{0,2}$"
 
Share this answer
 
Comments
gitika.khurana 16-Jul-12 4:25am    
Thanks, works just fine.

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