Matching a number

  • Thread starter =?ISO-8859-15?Q?Tobias_Schr=F6er?=
  • Start date
?

=?ISO-8859-15?Q?Tobias_Schr=F6er?=

Hi,

I'd like to validate a number input using a regular expression. This
would be done by "\d*". Now the input may have a fdecimal separator,
wich would be matched by "\d*(\.|,)\d*" (allowing the , as separator as
well).

What I need know is to ensure, that the total length of the string does
match a fixed length. Without the decimal separator this is trivial, but
with decimal separator I am stuck, as I am not too deep into regular
expression syntax.

TIA,
Tobi
 
J

Jon Skeet [C# MVP]

I'd like to validate a number input using a regular expression.

Does it *have* to be a regular expression? Are you doing this using
some component which requires a regular expression, or could you split
it into a regular expression and a length check?

Jon
 
?

=?ISO-8859-1?Q?Tobias_Schr=F6er?=

Jon said:
Does it *have* to be a regular expression? Are you doing this using
some component which requires a regular expression, or could you split
it into a regular expression and a length check?

Jon
No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different input
formats (strings, numbers with special input formats for each) and I'd
like the validation mechanism to be the same for all types if possible.
Therefore I ventured into regular expressions, which seen appropriate to
me for in this case.

Tobi
 
J

Jon Skeet [C# MVP]

No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different input
formats (strings, numbers with special input formats for each) and I'd
like the validation mechanism to be the same for all types if possible.
Therefore I ventured into regular expressions, which seen appropriate to
me for in this case.

I suggest you use a delegate to represent the validation - then it can
be as powerful as the client wants it to be, or as simple. You could
provide a regular expression delegate helper for cases where that's
sufficient.
 
?

=?ISO-8859-1?Q?Tobias_Schr=F6er?=

Jon said:
I suggest you use a delegate to represent the validation - then it can
be as powerful as the client wants it to be, or as simple. You could
provide a regular expression delegate helper for cases where that's
sufficient.

Thanks for your response.

Anyway, I'd be interested to know, if the desired validation could be
done by using a regEx. Something like: "(only digit characters and one
or none decimal separator, preceded and followed by at least one decimal
character) with a max length of n".

Tobi
 
J

Jesse Houwing

Hello Tobias,
Thanks for your response.

Anyway, I'd be interested to know, if the desired validation could be
done by using a regEx. Something like: "(only digit characters and one
or none decimal separator, preceded and followed by at least one
decimal character) with a max length of n".

Tobi

It can, but it isn't pretty:

^(?=.{0,10}$)\d+([,.]\d+)?$

It's using a forward look ahead to test the length and your normal pattern
to test the format. You can turn these around if needed:

^(?=\d+([,.]\d+)?$).{0,10}$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top