Reputation: 1
I am trying to find a regular expression to find out the number which is not entered as proper decimal or integer number in a input box
Examples
My current below allow me to catch all examples except example -3 where decimal dots can be repeated in any combination.
void T1_HTextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(T1_H.Text, "[^0-9.-]+|[.]{2}"))
{
MessageBox.Show("Please enter only numbers.");
T1_H.Text="";
}
}
Upvotes: 0
Views: 1538
Reputation: 18155
If you want to achieve this with Regular expression, you can use.
^(\d*\.)?\d+$
But please be aware that you can use Decimal.TryParse
as well. You can read more on Decimal.TryParse here
Upvotes: 0
Reputation: 8312
Try this regex:
^[0-9]+([.][0-9]{1,2})?$
Explanation:
Working example: https://regex101.com/r/iRaRPX/1/
It will check for all integers and decimal numbers up to two decimal points. You can change that according to your requirement.
Upvotes: 1
Reputation: 3080
If you really want to use a regexp you can use: ^[0-9]+(\.[0-9]+)?$
.
You can test it here https://regex101.com/r/UB6eRT/1
If you want to know if it's a valid number you can also try to convert it and check if you get an error.
Upvotes: 1