Click here to Skip to main content
14,423,055 members
Rate this:
Please Sign up or sign in to vote.
Hey guys I have a quick question maybe you could help. I'm trying to validate a phone number using RegEx but because the phonenumber has a + I can't seem to get it to work. I know best way is to use jquery or that but it is an old application.
Case eValidateInput.IsPhoneNumberWithPlus

                       Dim objRegExWithPlus As New System.Text.RegularExpressions.Regex("[+][0-9]{" & InputValue.Length - 1 & "}")
                       Dim objRegExWithNoPlus As New System.Text.RegularExpressions.Regex("[0-9]{" & InputValue.Length & "}")

                       If Not objRegExWithPlus.IsMatch(InputValue) And Not objRegExWithNoPlus.IsMatch(InputValue) Then
                           sValidationMsg = "'" & InputName & "' must be a numeric value"
                       End If


Cheers guys
Posted
Comments
ridoy 21-Aug-12 9:09am
   
give a format of phone number for what you are trying..
frostcox 21-Aug-12 9:28am
   
+222 345 2345 will be the format. Thanks
ridoy 21-Aug-12 13:08pm
   
I again ask you..is + is common for all number? and look,after first 3 digit(222) there is a space,then another 3 digit.I ask you this space is required or optional?
Rate this:
Please Sign up or sign in to vote.

Solution 2

You only need a single RegEx.
Use the conditional quantifiers in the Regex:
Case eValidateInput.IsPhoneNumberWithPlus
  Dim objRegEx As New System.Text.RegularExpressions.Regex("^\+?\s?\d{3}\s?\d{3}\s?\d{4}$")
 
  If Not objRegEx.IsMatch(InputValue) Then
    sValidationMsg = "'" & InputName & "' must be a numeric value"
  End If

Explanation of the Regex: ^\+?\s?\d{3}\s?\d{3}\s?\d{4}$
^       match must start at the beginning of the string
\+?     0 or 1 + character
\s?     0 or 1 whitespace character
\d{3}   exactly 3 decimal digits
\s?     0 or 1 whitespace character
\d{3}   exactly 3 decimal digits
\s?     0 or 1 whitespace character
\d{4}   exactly 4 decimal digits
$       match must end at the end of the string

This should match:
+222 345 6789
+222345 6789
+222 3456789
+2223456789
222 345 6789
222345 6789
222 3456789
2223456789
   
Comments
frostcox 21-Aug-12 18:19pm
   
Worked a treat thanks very much
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is what i m using:
it accepts below prefix
+91-9898989898 or +919898989898 or 09898989898 

/^(\+91-|\+91|0)?\d{10}$/

Specific on +91 then use,
/^(\+91)?\d{10}$/
   
v2
Comments
frostcox 21-Aug-12 8:35am
   
The format of my phone number will be +233 678 7890, so will this take care of this? cheers.
frostcox 21-Aug-12 8:51am
   
But the number won't always have a + sign
Kenneth Haugland 21-Aug-12 17:12pm
   
/^(\+91-|\+91|0|91)?\d{10}$/ ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100