Regular Expression Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jorell

    #1

    Regular Expression Help

    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
    regularexpressi onvalidator?? If so any help on what I
    would write for a regular expression?? Thank you


    Jorell
  • Chris Jackson

    #2
    Re: Regular Expression Help

    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" <zwarichj@nospa m.powervisionsw .com> wrote in message
    news:028301c355 2b$a0ff81c0$a10 1280a@phx.gbl.. .[color=blue]
    > 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
    > regularexpressi onvalidator?? If so any help on what I
    > would write for a regular expression?? Thank you
    >
    >
    > Jorell[/color]


    Comment

    • Jorell

      #3
      Re: Regular Expression Help

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

      Jorell

      [color=blue]
      >-----Original Message-----
      >Well, you can interrogate it on a number of levels - it[/color]
      really depends on[color=blue]
      >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[/color]
      characters 0 through 9, a[color=blue]
      >dash, or a comma. This would filter out any other[/color]
      character, however, you[color=blue]
      >would then approve the string ,-,,-,,-9-,,
      >
      >If you want to get a little bit closer, here is a better[/color]
      regex:[color=blue]
      >
      >^([0-9]+(\-[0-9]+)*,*)+$
      >
      >Now, you are required to have one or more numbers,[/color]
      followed by zero or more[color=blue]
      >instances of a dash followed by one or more numbers,[/color]
      followed by zero or[color=blue]
      >more instances of a comma - this entire sequence being[/color]
      repeated one or more[color=blue]
      >times.
      >
      >Now, you will match 1,2-9,13. You also guarantee that you[/color]
      will get at least[color=blue]
      >one number put in. However, this doesn't prevent you from[/color]
      putting in 9-2,[color=blue]
      >which may throw your parser off. It will also validate 0-[/color]
      2-4.[color=blue]
      >
      >So, you have to sit and play with exactly what you will[/color]
      allow, and gradually[color=blue]
      >keep expanding what you are doing until you have a great[/color]
      validation in place[color=blue]
      >that covers every instance that you can come up with.
      >
      >If your parser is very particular about how the input[/color]
      comes in, then you may[color=blue]
      >want to write a custom validator in code.
      >
      >--
      >Chris Jackson
      >Software Engineer
      >Microsoft MVP - Windows XP
      >Windows XP Associate Expert
      >--
      >"Jorell" <zwarichj@nospa m.powervisionsw .com> wrote in[/color]
      message[color=blue]
      >news:028301c35 52b$a0ff81c0$a1 01280a@phx.gbl. ..[color=green]
      >> Hi everyone. I have never worked with regular[/color][/color]
      expressions[color=blue][color=green]
      >> 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
      >> regularexpressi onvalidator?? If so any help on what I
      >> would write for a regular expression?? Thank you
      >>
      >>
      >> Jorell[/color]
      >
      >
      >.
      >[/color]

      Comment

      • William F. Robertson, Jr.

        #4
        Re: Regular Expression Help

        Here are a couple articles to help you get going.

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



        http://www.c-sharpcorner.com/Code/20...mValidator.asp

        HTH,

        bill

        "Jorell" <zwaricgj@nospa m.powervisionsw .com> wrote in message
        news:029e01c355 41$2d4a5ff0$a40 1280a@phx.gbl.. .[color=blue]
        > Thank you very much!!! Do you happen to know where a
        > person can learn a little more about creating those??
        > Thanks in advance!!
        >
        > Jorell
        >
        >[color=green]
        > >-----Original Message-----
        > >Well, you can interrogate it on a number of levels - it[/color]
        > really depends on[color=green]
        > >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[/color]
        > characters 0 through 9, a[color=green]
        > >dash, or a comma. This would filter out any other[/color]
        > character, however, you[color=green]
        > >would then approve the string ,-,,-,,-9-,,
        > >
        > >If you want to get a little bit closer, here is a better[/color]
        > regex:[color=green]
        > >
        > >^([0-9]+(\-[0-9]+)*,*)+$
        > >
        > >Now, you are required to have one or more numbers,[/color]
        > followed by zero or more[color=green]
        > >instances of a dash followed by one or more numbers,[/color]
        > followed by zero or[color=green]
        > >more instances of a comma - this entire sequence being[/color]
        > repeated one or more[color=green]
        > >times.
        > >
        > >Now, you will match 1,2-9,13. You also guarantee that you[/color]
        > will get at least[color=green]
        > >one number put in. However, this doesn't prevent you from[/color]
        > putting in 9-2,[color=green]
        > >which may throw your parser off. It will also validate 0-[/color]
        > 2-4.[color=green]
        > >
        > >So, you have to sit and play with exactly what you will[/color]
        > allow, and gradually[color=green]
        > >keep expanding what you are doing until you have a great[/color]
        > validation in place[color=green]
        > >that covers every instance that you can come up with.
        > >
        > >If your parser is very particular about how the input[/color]
        > comes in, then you may[color=green]
        > >want to write a custom validator in code.
        > >
        > >--
        > >Chris Jackson
        > >Software Engineer
        > >Microsoft MVP - Windows XP
        > >Windows XP Associate Expert
        > >--
        > >"Jorell" <zwarichj@nospa m.powervisionsw .com> wrote in[/color]
        > message[color=green]
        > >news:028301c35 52b$a0ff81c0$a1 01280a@phx.gbl. ..[color=darkred]
        > >> Hi everyone. I have never worked with regular[/color][/color]
        > expressions[color=green][color=darkred]
        > >> 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
        > >> regularexpressi onvalidator?? If so any help on what I
        > >> would write for a regular expression?? Thank you
        > >>
        > >>
        > >> Jorell[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...