Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how i make regular expression for phone number accepts only number and start with 0 or 00 or + only,thanks alot
Posted

Have a look at below useful article on "Regular Expression".
The 30 Minute Regex Tutorial

This may not have exact code of your requirement, but I am sure it will help you to learn very useful topic like "Regular Expression".
 
Share this answer
 
Use this
^(0{1,2}|\+)(\d)+$
You can specify count of digits too if you wish.
^(0{1,2}|\+)(\d){10}$
 
Share this answer
 
Comments
Vikas_Shukla_89 24-Oct-11 5:42am    
nice one, my +5
Amir Mahfoozi 24-Oct-11 8:15am    
Do you know that your pattern accepts solar system and milkyway galaxy codes in addition to area and country codes ? for example it accepts 00001 !
 
Share this answer
 
try this one too :
^([0]{1,2}|\+)[1-9]\d{7,10}$

7 = minimum number of digits after first nonzero gigit
10 = maximum number of digits after first nonzero gigit
 
Share this answer
 
v3
Comments
Prerak Patel 24-Oct-11 6:41am    
[1-9]{7,10}??! Who said that a phone number cannot have 0. Better use \d. See my answer.
Amir Mahfoozi 24-Oct-11 7:23am    
please revise your vote.
Prerak Patel 24-Oct-11 7:38am    
Alright...
Amir Mahfoozi 24-Oct-11 7:20am    
you are right, I was going to change that but you commented first.
To validate Text Filed for valid PhoneNumber using Vb.Net
Set the Maximum length for Textbox Control equal to 15.
VB
Public Sub ValidPhoneNumber(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Select Case Asc(e.KeyChar) 'Validating PhoneNumber using Ascii
            Case 8, 45, 43, 48 To 57  'if Ascii is 48...57 then allow
                e.Handled = False
            Case Else                 'Else Providing Message 
                e.Handled = True
                MessageBox.Show("Please Enter Number's Only", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Select
    End Sub

Call the above method on Texbox KeyPress Event.
It allows following types of Phone Numbers
022-2123456
91-0123456789
+91-012345678
0123456789
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900