Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to do a validation to a text box which is a phone number which accepts numbers,symbols like "+","-",empty space

e.g +49 561 56789-0

VB
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) < 48) _
               Or Asc(e.KeyChar) > 57) Then
            e.Handled = True
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
end sub


I know this would check only numbers but i need to include two symbols "+","-"...Not sure how do i do this.


Please help. Thanks.
Posted
Updated 30-Jul-13 8:12am
v2
Comments
Manfred Rudolf Bihy 30-Jul-13 13:32pm    
And your question is?
What have you tried and what problems have you encountered?
Did Google deny you any results from your searches? (hint)
vidkaat 30-Jul-13 14:12pm    
updated question
[no name] 30-Jul-13 13:46pm    
"Please help", with what? Did you try any of the solutions to the exact same "question" that you are asking? Did they not work for you? Show us the code that you are having trouble with.
vidkaat 30-Jul-13 14:13pm    
question updated
[no name] 30-Jul-13 14:17pm    
Okay.... Lookup the ASCII value for + and - and add two more Or conditions to your If, that is if you want to continue with the same convention.

1 solution

Have you ever heard about RegEx[^]?
All you need to do is to use IsMatch[^] method with pattern to telephone number.

Example patterns you'll find here:
http://regexlib.com/DisplayPatterns.aspx?categoryId=7&AspxAutoDetectCookieSupport=1[^]
http://www.regular-expressions.info/examples.html[^]
 
Share this answer
 

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