tags:

views:

95

answers:

6

Hi,

i want to validate the input of a text box so as not be empty and also to accept only decimal or integer. I have tried the following regex's: ^\S[0-9],?[0-9]$ this one allows one letter at the beginning

^\S[0-9]+,?[0-9]*$ this one althought that does not allow letters, it requires for at least 2 numbers which is not desired.

thank you very much in advance for your time.

A: 

This should work ^\d{1,}.?\d{1,}$

Phil
{1,} could be simplified as + and this doesn't allow "1" to be entered.
Yuriy Faktorovich
Unfortunately it does not work. It will require at least two digits.
klausbyskov
you probably want to escape the `.`, as it's matching any character right now
Chad
+4  A: 
^\d+(\.\d+)?$

Starts with a digit, then possibly . more digits. If you'd like negative numbers

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

Although easier and more useful might be Double.TryParse.

Yuriy Faktorovich
What about negatives?
Chad
@Chad - OP made no mention of negatives, nor did his attempt at a solution indication any desire to do so.
Joel Etherton
@Joel Etherton, he said integer. Integers include negatives.
Chad
@Chad - maybe in a specific programming context, but in the context of the question, in addition to his attempt at a solution which does not consider them, negatives would be out of scope. That's not to knock your solution which does include them, but your comment to Yuriy for not including them is out of bounds. His answer meets the conditions of the question, both explicit and implied.
Joel Etherton
@Joel Etherton, I infered, possibly, by the use of the '\S' at the start of the OPs attempted regex that maybe they were attempting to use them. I wouldn't say my comment is out of bounds, I wasn't rude about it. I just asked "What about negatives?" as I thought the OP would want them if they are looking for integers.
Chad
+1  A: 

"^\d+(\.\d+)?$" ought to do it. This will match a string beginning with one or more digits, then optionally a decimal point and one or more additional digits. The decimal point and fractional part are grouped so if you have one, you need the other as well, but you can have neither.

The use of \S will match any non-whitespace character at the beginning, which is probably not what you want.

KeithS
+3  A: 

I'd recommend just using a compare validator...

  <asp:CompareValidator id="Compare1" 
       ControlToValidate="TextBox1" 
       Operator="DataTypeCheck"
       Type="Double"
       runat="server"/>

It's designed to do the type of thing you are talking about without messing with all the regex...

I'll admit i'm not a fan of regex (unless truly needed). Take a look at this article:

Regular Expressions: Now You Have Two Problems

Abe Miessler
A: 

^[\d]{1,99}[.]\d{1,99}

Is pretty much what you need. You can try it out here: http://www.gskinner.com/RegExr/

J Angwenyi
A: 

This will allow positive or negative numbers, with commas, and decimals.

However, it doesn't ensure commas are in the correct places

^-?[\d,]+\.?\d*$

passing

  • 2
  • 10
  • 10,000
  • 10,000.00
  • -10
  • -10,000
  • -10,000.00

failing

  • a123
  • asdf
  • 123a

To be honest though, regex is the wrong solution for this

Chad