I have a textbox that a user enters a number into. I need to ensure that the number is at most 5 numbers before the decimal place and mandatory 2 digits after. The number must always have 2 digits after the decimal point. What Regex could I use to check this? (The solution is in C#)
Asked
Active
Viewed 4,154 times
1
-
1have you looked at the regex generators online? It is something like: `[0-9]+.{2}[0-9]` – Callum Linington Jun 07 '16 at 07:41
-
Refer this could be possible duplicate with two digits http://stackoverflow.com/questions/1014284/regex-to-match-2-digits-optional-decimal-two-digits – Rahul Hendawe Jun 07 '16 at 07:43
2 Answers
7
Something like this:
String source = ...;
if (Regex.IsMatch(source, @"^[0-9]{,5}\.[0-9]{2}$")) {
//TODO: put relevant code here
}
If you want at least one digit before decimal point, the pattern will be
@"^[0-9]{1,5}\.[0-9]{2}$"

Dmitry Bychenko
- 180,369
- 20
- 160
- 215
1
Just Try this code
string Value= "12345.63";
if (Regex.IsMatch(Value, @"^[0-9]{5}\.[0-9]{2}$"))
{
Console.WriteLine(Value);
}
else
{
Console.WriteLine("Not Match");
}
Console.ReadKey();

Badruzzaman
- 41
- 1
- 7