Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Can anyone explain why this code is not working.

I use two decimal digits.


54.57
123.43
0.34


C#
string sal = txtSalary.Text;

Regex regexSal = new Regex(@"/^\d{1,9}(?:\.\d{0,2})?$/");

Match matchSal = regexSal.Match(sal);



Why its not matching
Posted

^\d{1,4}(\.\d{1,2})?$
it's the regex for 2 decimal.
 
Share this answer
 
Take out the leading and trailing slashes:
Regex regexSal = new Regex(@"^\d{1,9}(?:\.\d{0,2})?$");
 
Share this answer
 
Comments
KUMAR619 27-Jun-14 7:27am    
Well Said.
Thank you Sir
Start with 1 to 9 digits:
^\d{1,9}

followed by a .
\.

then
end with 2 decimals compulsory:
\d{2}$

putting them together:
^\d{1,9}\.\d{2}$

in C#, it becomes:
@"^\d{1,9}\.\d{2}$"
 
Share this answer
 
v2
use this Expression

^\d{1,9}(?:\.\d{0,2})?$
 
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