Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Regex - specify a max. length?

Regex - specify a max. length?

Scheduled Pinned Locked Moved .NET (Core and Framework)
regextutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    boy pockets
    wrote on last edited by
    #1

    Hi, I have regular expression for a phone number. The rules i want are; 10 and only 10 digits and any characters in between. eg. "0123456789" "(01)23456789" "(01)23 456 789 "0123 456 789 "0 1 2 3 4 5 6 7 8 9" "01 23 45 67 89" are all acceptable the regex i have is: "([^0-9]{0,}[0-9][^0-9]{0,}){10}" everything works ok except for detecting numbers that are too long (i.e. more than ten characters). I already have a work-around for this, i am just interested to know how to achieve the same thing in a regex. cheers to you.

    G M 2 Replies Last reply
    0
    • B boy pockets

      Hi, I have regular expression for a phone number. The rules i want are; 10 and only 10 digits and any characters in between. eg. "0123456789" "(01)23456789" "(01)23 456 789 "0123 456 789 "0 1 2 3 4 5 6 7 8 9" "01 23 45 67 89" are all acceptable the regex i have is: "([^0-9]{0,}[0-9][^0-9]{0,}){10}" everything works ok except for detecting numbers that are too long (i.e. more than ten characters). I already have a work-around for this, i am just interested to know how to achieve the same thing in a regex. cheers to you.

      G Offline
      G Offline
      gericooper
      wrote on last edited by
      #2

      handle keypress event count the pressed in chars. if =10 e.Handled = true. Hope this helps cheers, geri

      G 1 Reply Last reply
      0
      • G gericooper

        handle keypress event count the pressed in chars. if =10 e.Handled = true. Hope this helps cheers, geri

        G Offline
        G Offline
        gericooper
        wrote on last edited by
        #3

        so I would do sg like int count=0; if(Char.IsNumber(e.KeyChar)) { count++; } if(count==10) e.Handled=true; //no more chars allowed to be preesed in. & count-- if user deletes.....(so presses like: e.KeyChar = '\u0008' >> backspace, i guess ) geri -- modified at 2:46 Thursday 29th November, 2007

        B 1 Reply Last reply
        0
        • G gericooper

          so I would do sg like int count=0; if(Char.IsNumber(e.KeyChar)) { count++; } if(count==10) e.Handled=true; //no more chars allowed to be preesed in. & count-- if user deletes.....(so presses like: e.KeyChar = '\u0008' >> backspace, i guess ) geri -- modified at 2:46 Thursday 29th November, 2007

          B Offline
          B Offline
          boy pockets
          wrote on last edited by
          #4

          Hi, thanks for the reply, but my question was specifically to do with regular expressions. thanks anyway.

          1 Reply Last reply
          0
          • B boy pockets

            Hi, I have regular expression for a phone number. The rules i want are; 10 and only 10 digits and any characters in between. eg. "0123456789" "(01)23456789" "(01)23 456 789 "0123 456 789 "0 1 2 3 4 5 6 7 8 9" "01 23 45 67 89" are all acceptable the regex i have is: "([^0-9]{0,}[0-9][^0-9]{0,}){10}" everything works ok except for detecting numbers that are too long (i.e. more than ten characters). I already have a work-around for this, i am just interested to know how to achieve the same thing in a regex. cheers to you.

            M Offline
            M Offline
            mabo42
            wrote on last edited by
            #5

            Put $ at the end: "([^0-9]{0,}[0-9][^0-9]{0,}){10}$" Regards

            F 1 Reply Last reply
            0
            • M mabo42

              Put $ at the end: "([^0-9]{0,}[0-9][^0-9]{0,}){10}$" Regards

              F Offline
              F Offline
              Fahad Sadah
              wrote on last edited by
              #6

              This would have no effect, the beginning is not limited. "^([^0-9]{0,}[0-9][^0-9]{0,}){10}$" fixes this. (in your regex, the number 12345678901 would have caused the underlined part to match.

              M B 2 Replies Last reply
              0
              • F Fahad Sadah

                This would have no effect, the beginning is not limited. "^([^0-9]{0,}[0-9][^0-9]{0,}){10}$" fixes this. (in your regex, the number 12345678901 would have caused the underlined part to match.

                M Offline
                M Offline
                mabo42
                wrote on last edited by
                #7

                oops, absolutly right Regards

                1 Reply Last reply
                0
                • F Fahad Sadah

                  This would have no effect, the beginning is not limited. "^([^0-9]{0,}[0-9][^0-9]{0,}){10}$" fixes this. (in your regex, the number 12345678901 would have caused the underlined part to match.

                  B Offline
                  B Offline
                  boy pockets
                  wrote on last edited by
                  #8

                  Thanks, that works just as i need it. I had tried the $ character at the end, but not in conjunction with ^. Thanks to Mabo42 to the reply as well.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups