Syntax for regular expression that is a number?

  • Thread starter Thread starter Mark B
  • Start date Start date
M

Mark B

What would the regular expression syntax be to validate that a field's input
is simply any integer number >=1?
 
Alexey Smirnov wrote :
Mark, here it is: ^\d+$

not quite, as that would allow "0", which was not the question.

use: ^[1-9]\d*$
this does not allow leading zeroes or a single zero.

Hans Kesting
 
Alexey Smirnov wrote :
Mark, here it is: ^\d+$

not quite, as that would allow "0", which was not the question.

use: ^[1-9]\d*$
this does not allow leading zeroes or a single zero.

Hans Kesting

Ah, you're right, your pattern is correct, thanks
 
re:
!> Mark, here it is: ^\d+$

I don't think so.

This regex defines "any positive integer number >=1" :

^[1-9]+[0-9]*$

Using ASP.NET's range validator control will make the job easier, though.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
=========================
What would the regular expression syntax be to validate that a field's input
is simply any integer number >=1?

Mark, here it is: ^\d+$
 
Hans said:
Alexey Smirnov wrote :
Mark, here it is: ^\d+$

not quite, as that would allow "0", which was not the question.

use: ^[1-9]\d*$
this does not allow leading zeroes or a single zero.

But 007 satisfies the OP's requirement...

Andrew
 
Thanks all.

Actually we wouldn't want 007. It's just a text field on a page for the user
to enter the number of users they want their software license to cover.

I can't bring myself to put an upper limit on it (e.g. using the range
validator) so I decided to go for ^[1-9]\d*$.



Andrew Morton said:
Hans said:
Alexey Smirnov wrote :
What would the regular expression syntax be to validate that a
field's input is simply any integer number >=1?

Mark, here it is: ^\d+$

not quite, as that would allow "0", which was not the question.

use: ^[1-9]\d*$
this does not allow leading zeroes or a single zero.

But 007 satisfies the OP's requirement...

Andrew
 
Hello Mark,
Thanks all.

Actually we wouldn't want 007. It's just a text field on a page for
the user to enter the number of users they want their software license
to cover.

I can't bring myself to put an upper limit on it (e.g. using the range
validator) so I decided to go for ^[1-9]\d*$.

If you have a datatype in use (say an int), you will have an upper limit
enforced by the number of bytes...

Just a thought... this regex can cause parsing problems of someone were to
type:

99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

and hit enter...

Jesse

Hans said:
Alexey Smirnov wrote :


What would the regular expression syntax be to validate that a
field's input is simply any integer number >=1?

Mark, here it is: ^\d+$

not quite, as that would allow "0", which was not the question.

use: ^[1-9]\d*$
this does not allow leading zeroes or a single zero.
But 007 satisfies the OP's requirement...

Andrew
 
Back
Top