views:

489

answers:

6

Regular expression to validate a text box where i can enter an integer / float value in asp.net

+4  A: 

Try this:

^\d*\.?\d+$

Edit: Fredrik Mörk made and excellent suggestion to make this expression culturally-aware. Build the expression string like this:

String regex = String.Format("^\d*\{0}?\d+$", 
                             CultureInfo
                             .CurrentCulture
                             .NumberFormat
                             .CurrencyDecimalSeparator);
Andrew Hare
It may be that you would want to be more dynamic around the choice of decimal separator so that it is compatible with other locales/cultures as well.
Fredrik Mörk
This will not match plain single-digit integers. (i.e. 5) or floats less than 1. (i.e. .5)
RC
@RC - This expression matches both "5" and ".5"
Andrew Hare
Yea, it was the leading \d+ that was getting you. How were you able to edit your answer without it stating you did so? I don't mine, just curious?
RC
Should it match negative numbers too?
Alex Barrett
@Alex - I am not sure - the OP didn't indicate whether or not that was a requirement.
Andrew Hare
@RC, edits done in the first 5-ish minutes since the last edit (or creation) don't get counted as updates. I gather that it's to catch those "Oh my God, I can't believe how many spelling mistakes I made in my haste to get first answer" moments.
paxdiablo
Although the rep drive to get first answer has disappeared somewhat now that equal-voted answers appear in random order rather than based on last-edit time.
paxdiablo
@RC - Sorry, I didn't see your second comment! @Pax - thanks for answering :)
Andrew Hare
+1  A: 

^[-+]?[0-9]*\.?[0-9]+$

x2
You need to escape the "." in the middle (`\.`). As it is now it is matching any character.
Andrew Hare
Thanks for you comment, parser ate my slash :(
x2
+3  A: 
^[-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$

Will match the following (examples):

3.4
34.34
45345
-34
.55
-.45
-2.2
1.0e-10
45.
1.e308
Kirill V. Lyadvinsky
but won't match "45."
Lucky
You are right. Fixed.
Kirill V. Lyadvinsky
+1  A: 

the code

^[\d.]+$

matches multiple dots in strings like "1.1.1"

try

^\d+(.\d+)?$

instead

you should also note that in some countries the comma is used instead of the dot.

João Portela
+4  A: 

As you can see from the various answers, Regexes can add unneeded complexity. float.TryParse() will tell you for certain whether the value can be made a float or not.

This also takes account of regional settings on the users' machine, which regular expressions won't (or will get ugly if they try).

I'd consider using something like this instead of a Regex:

bool isValid = float.TryParse(textbox1.Text);
Neil Barnwell
I agree in principle but since this is client input validation the OP may be relying on using the expression in JavaScript if they intend on doing the validation client-side.
Andrew Hare
An excellent point. Hadn't thought of that. I would say though, that Javascript validation should always be backed up by server-side validation anyway, in case JS is turned off in the client, or a bot is spamming requests.
Neil Barnwell
+1  A: 

Why not use a CompareValidator to verify that the value is a number?

<asp:TextBox ID="numberTextBox" runat="server" /> 
<asp:CompareValidator ID="validator" runat="server" ControlToValidate="numberTextBox" Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a number" />
jrummell