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. Web Development
  3. ASP.NET
  4. RegEx to allow Phone Numbers with #-###-###-#### or ###-###-#### format only

RegEx to allow Phone Numbers with #-###-###-#### or ###-###-#### format only

Scheduled Pinned Locked Moved ASP.NET
regexquestion
4 Posts 3 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.
  • V Offline
    V Offline
    Vipul Mehta
    wrote on last edited by
    #1

    Hi, I wanted a RegEx to allow user to enter phone number in #-###-###-#### or ###-###-#### format only. (Eg: 1-888-456-5678 or 888-456-5678) I have implemented RegEx[1] to allow user to enter Phone Number in #-###-###-#### format. What changes do we need to make to the existing RegEx so that it allows user to enter Phone Number in the above 2 formats only? Thanks in advance. [1] @"^\d{1}-\d{3}-\d{3}-\d{4}$" Regards, Vipul Mehta

    Regards, Vipul Mehta

    D A 2 Replies Last reply
    0
    • V Vipul Mehta

      Hi, I wanted a RegEx to allow user to enter phone number in #-###-###-#### or ###-###-#### format only. (Eg: 1-888-456-5678 or 888-456-5678) I have implemented RegEx[1] to allow user to enter Phone Number in #-###-###-#### format. What changes do we need to make to the existing RegEx so that it allows user to enter Phone Number in the above 2 formats only? Thanks in advance. [1] @"^\d{1}-\d{3}-\d{3}-\d{4}$" Regards, Vipul Mehta

      Regards, Vipul Mehta

      D Offline
      D Offline
      Dinesh Mani
      wrote on last edited by
      #2

      You can just add an OR and put the 2nd type like so - "^\d{1}-\d{3}-\d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$" HTH!

      V 1 Reply Last reply
      0
      • D Dinesh Mani

        You can just add an OR and put the 2nd type like so - "^\d{1}-\d{3}-\d{3}-\d{4}$|^\d{3}-\d{3}-\d{4}$" HTH!

        V Offline
        V Offline
        Vipul Mehta
        wrote on last edited by
        #3

        Thanks Dinesh!! This works well

        Regards, Vipul Mehta

        1 Reply Last reply
        0
        • V Vipul Mehta

          Hi, I wanted a RegEx to allow user to enter phone number in #-###-###-#### or ###-###-#### format only. (Eg: 1-888-456-5678 or 888-456-5678) I have implemented RegEx[1] to allow user to enter Phone Number in #-###-###-#### format. What changes do we need to make to the existing RegEx so that it allows user to enter Phone Number in the above 2 formats only? Thanks in advance. [1] @"^\d{1}-\d{3}-\d{3}-\d{4}$" Regards, Vipul Mehta

          Regards, Vipul Mehta

          A Offline
          A Offline
          Arindam Tewary
          wrote on last edited by
          #4

          Your regex should be like this

          ^(\d{1}-){0,1}\d{3}-\d{3}-\d{4}$

          to allow it to capture mentioned two pattern and not any others. Please let me know if it helps you.

          //String input = "888-456-5678";
          String input = "1-888-456-5678";

                  String sPattern = @"^(\\d{1}-){0,1}\\d{3}-\\d{3}-\\d{4}$";
                  Regex o = new Regex(sPattern);
                  Match m= o.Match(input);
                  if (m.Success)
                  {
                      MessageBox.Show("Match found.");
                  }
                  else
                  {
                      MessageBox.Show("No match");
                  }
          

          Thanks, Arindam D Tewary

          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