Regular Expression Help

  • Thread starter Thread starter Jorell
  • Start date Start date
J

Jorell

Hi everyone. I have never worked with regular expressions
before and here is my dillema:

I have a textbox that I want to only accept this type of
input:

1,2,3-8,10,16 ( Comma separated numbers or Hyphenated
ranges )

I want to use a validator for this, so I assume I use a
regularexpressionvalidator?? If so any help on what I
would write for a regular expression?? Thank you


Jorell
 
Well, you can interrogate it on a number of levels - it really depends on
how thoroughly you want to make your validation.

I mean, you could validate the characters themselves:

^[0-9\-,]*$

What this means: you will have zero or more of the characters 0 through 9, a
dash, or a comma. This would filter out any other character, however, you
would then approve the string ,-,,-,,-9-,,

If you want to get a little bit closer, here is a better regex:

^([0-9]+(\-[0-9]+)*,*)+$

Now, you are required to have one or more numbers, followed by zero or more
instances of a dash followed by one or more numbers, followed by zero or
more instances of a comma - this entire sequence being repeated one or more
times.

Now, you will match 1,2-9,13. You also guarantee that you will get at least
one number put in. However, this doesn't prevent you from putting in 9-2,
which may throw your parser off. It will also validate 0-2-4.

So, you have to sit and play with exactly what you will allow, and gradually
keep expanding what you are doing until you have a great validation in place
that covers every instance that you can come up with.

If your parser is very particular about how the input comes in, then you may
want to write a custom validator in code.
 
Thank you very much!!! Do you happen to know where a
person can learn a little more about creating those??
Thanks in advance!!

Jorell

-----Original Message-----
Well, you can interrogate it on a number of levels - it really depends on
how thoroughly you want to make your validation.

I mean, you could validate the characters themselves:

^[0-9\-,]*$

What this means: you will have zero or more of the characters 0 through 9, a
dash, or a comma. This would filter out any other character, however, you
would then approve the string ,-,,-,,-9-,,

If you want to get a little bit closer, here is a better regex:

^([0-9]+(\-[0-9]+)*,*)+$

Now, you are required to have one or more numbers, followed by zero or more
instances of a dash followed by one or more numbers, followed by zero or
more instances of a comma - this entire sequence being repeated one or more
times.

Now, you will match 1,2-9,13. You also guarantee that you will get at least
one number put in. However, this doesn't prevent you from putting in 9-2,
which may throw your parser off. It will also validate 0- 2-4.

So, you have to sit and play with exactly what you will allow, and gradually
keep expanding what you are doing until you have a great validation in place
that covers every instance that you can come up with.

If your parser is very particular about how the input comes in, then you may
want to write a custom validator in code.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
Hi everyone. I have never worked with regular expressions
before and here is my dillema:

I have a textbox that I want to only accept this type of
input:

1,2,3-8,10,16 ( Comma separated numbers or Hyphenated
ranges )

I want to use a validator for this, so I assume I use a
regularexpressionvalidator?? If so any help on what I
would write for a regular expression?? Thank you


Jorell


.
 
Here are a couple articles to help you get going.

Look closely at the EvaluationFunction attribute and the EvaluateIsValid
method (or function)

http://www.codeproject.com/useritems/DateValidator.asp

http://www.c-sharpcorner.com/Code/2002/May/DateCustomValidator.asp

HTH,

bill

Jorell said:
Thank you very much!!! Do you happen to know where a
person can learn a little more about creating those??
Thanks in advance!!

Jorell

-----Original Message-----
Well, you can interrogate it on a number of levels - it really depends on
how thoroughly you want to make your validation.

I mean, you could validate the characters themselves:

^[0-9\-,]*$

What this means: you will have zero or more of the characters 0 through 9, a
dash, or a comma. This would filter out any other character, however, you
would then approve the string ,-,,-,,-9-,,

If you want to get a little bit closer, here is a better regex:

^([0-9]+(\-[0-9]+)*,*)+$

Now, you are required to have one or more numbers, followed by zero or more
instances of a dash followed by one or more numbers, followed by zero or
more instances of a comma - this entire sequence being repeated one or more
times.

Now, you will match 1,2-9,13. You also guarantee that you will get at least
one number put in. However, this doesn't prevent you from putting in 9-2,
which may throw your parser off. It will also validate 0- 2-4.

So, you have to sit and play with exactly what you will allow, and gradually
keep expanding what you are doing until you have a great validation in place
that covers every instance that you can come up with.

If your parser is very particular about how the input comes in, then you may
want to write a custom validator in code.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
Hi everyone. I have never worked with regular expressions
before and here is my dillema:

I have a textbox that I want to only accept this type of
input:

1,2,3-8,10,16 ( Comma separated numbers or Hyphenated
ranges )

I want to use a validator for this, so I assume I use a
regularexpressionvalidator?? If so any help on what I
would write for a regular expression?? Thank you


Jorell


.
 
Back
Top