RegEx to allow Phone Numbers with #-###-###-#### or ###-###-#### format only
-
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
-
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
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! -
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!Thanks Dinesh!! This works well
Regards, 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
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