IT KNOWLEDGE

IT KNOWLEDGE
BC

Friday, May 23, 2014

How to Validate Phone Number (VB.Net)

This tutorial will help you to validate phone number with two format:

  • 000-000-000                 -eg: 012-000-999
  • 000-000-0000               -eg: 012-000-9999
  • (000)00-000-000          -eg: (855)12-000-999
  • (000)00-000-0000        -eg: (855)12-000-9999
Application Design

No Control Type Name Text
1 Form1 FormValidate Validate Phone Number
2 Textbox1 TextBoxPhone
3 Label1 LabelStatus
4 Button1 ButtonValidate Validate

Code
Imports System.Text.RegularExpressions
Public Class FormValidate

    Private Sub ButtonValidate_Click(sender As Object, e As EventArgs) Handles ButtonValidate.Click
        Dim phoneNumber As New Regex("\d{3}-\d{3}-\d{4}")
        Dim phoneNumber1 As New Regex("\d{3}-\d{3}-\d{3}")
        Dim phoneNumber2 As New Regex("\(\d{3}\)\d{2}-\d{3}-\d{3}")
        Dim phoneNumber3 As New Regex("\(\d{3}\)\d{2}-\d{3}-\d{4}")
        If phoneNumber.IsMatch(TextBoxPhone.Text) Then
            LabelStatus.Text = "The phone number: " & TextBoxPhone.Text & " is validated"
        ElseIf phoneNumber1.IsMatch(TextBoxPhone.Text) Then
            LabelStatus.Text = "The phone number: " & TextBoxPhone.Text & " is validated"
        ElseIf phoneNumber2.IsMatch(TextBoxPhone.Text) Then
            LabelStatus.Text = "The phone number: " & TextBoxPhone.Text & " is validated"
        ElseIf phoneNumber3.IsMatch(TextBoxPhone.Text) Then
            LabelStatus.Text = "The phone number: " & TextBoxPhone.Text & " is validated"
        Else
            LabelStatus.Text = "The phone number: " & TextBoxPhone.Text & " is not validated"
        End If
    End Sub

End Class

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: itech-9999

0 comments: