By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Regular Expression Help

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
--
"Jorell" <zw******@nospam.powervisionsw.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 17 '05 #2

P: n/a
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 onhow 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, adash, or a comma. This would filter out any other character, however, youwould 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 moreinstances of a dash followed by one or more numbers, followed by zero ormore instances of a comma - this entire sequence being repeated one or moretimes.

Now, you will match 1,2-9,13. You also guarantee that you will get at leastone 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 graduallykeep expanding what you are doing until you have a great validation in placethat covers every instance that you can come up with.

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

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows XP
Windows XP Associate Expert
--
"Jorell" <zw******@nospam.powervisionsw.com> wrote in messagenews:02****************************@phx.gbl...
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

.

Nov 17 '05 #3

P: n/a
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/20...mValidator.asp

HTH,

bill

"Jorell" <zw******@nospam.powervisionsw.com> wrote in message
news:02****************************@phx.gbl...
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
--
"Jorell" <zw******@nospam.powervisionsw.com> wrote in

message
news:02****************************@phx.gbl...
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

.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.