问题
I'm trying to make a Regular expression for a textbox which should only allow either only digits or decimals. For example, I should be able to enter both 10 and 10,350.
The decimal seperator I'll use is "," and the decimal length don't need a limit.
Anyone knows how I can make such a RegEx?
回答1:
I'd go for decimal.TryParse()
too, but if you really need a RegEx then something like this should work: (\d+(,\d*)?)
回答2:
Just use decimal.TryParse
or double.TryParse
.
回答3:
if you will use it in many place in your project you can make new TextBoxcontrol it accept just number and make ',' when even the '.' clicked, and you can make test on it when it lostfocus()
回答4:
Here's your regex:
^[\d]+,?[\d]*$
check if it matches what's in your textbox.
来源:https://stackoverflow.com/questions/6087620/c-sharp-regex-for-decimal-number