Question: I have a regular form(not windows form) in Visual Studio using vb.net. I would like to validate the phone field once it looses focus, in the sense when the user tabs out of the field. How do I raise an event? The dropdown doesn't come with lostfocus event?
Thanks.
~ Nita

Answer: 
You can use the method below to validate phone number. Just pass a phone number argument into the method.

```
 Protected Sub txtPhone_TextChanged(sender As Object, e As EventArgs) Handles txtPhone.TextChanged
    If Not IsPhoneNumberValid(txtPhone.Text) Then
        Dim isvalid = False
        lblValidatioMessage.Visible = True
        lblValidatioMessage.Text = "*Invalid Phonenumber"
    Else
        lblValidatioMessage.Visible = False
        lblValidatioMessage.Text = ""
    End If
End Sub

Private Shared Function IsPhoneNumberValid(phoneNumber As String) As Boolean
    Dim result As String = ""
    Dim chars As Char() = phoneNumber.ToCharArray()
    For count = 0 To chars.GetLength(0) - 1
        Dim tempChar As Char = chars(count)
        If [Char].IsDigit(tempChar) Or "()+-., ".Contains(tempChar.ToString()) Then

            result += StripNonAlphaNumeric(tempChar)
        Else
            Return False
        End If

    Next

    Return result.Length = 10 'Length of US phone numbers is 10
End Function

Private Shared Function StripNonAlphaNumeric(value As String) As String
    Dim regex = New Regex("[^0-9a-zA-Z]", RegexOptions.None)
    Dim result As String = ""
    If regex.IsMatch(value) Then
        result = regex.Replace(value, "")
    Else
        result = value
    End If

    Return result
End Function

```

and in your code front

```
<asp:Label ID="lblPhone" runat="server" Text="Phone"></asp:Label>
<p><asp:TextBox ID="txtPhone" runat="server" AutoPostBack="True"></asp:TextBox></p>
<asp:Label ID="lblValidatioMessage" Visible="False" runat="server" Text="" ForeColor="red"></asp:Label>

```
```
Private Function IsPhoneNumberValid(ByVal Number As String) As Boolean

    dim PhoneValid As boolean
    Dim PhoneNumber As String = "^[1-9]\d{2}-[1-9]\d{2}-\d{4}$"
    Dim ChekPhone As New Regex(PhoneNumber )
    If Not String.IsNullOrEmpty(Number ) Then

        PhoneValid = ChekPhone.IsMatch(Number ) 
 Else
        PhoneValid = False 

    End If

    Return PhoneValid 
End Function

Private Sub txtPhoneNumber_LostFocus(sender As Object, e As System.EventArgs) Handles txtPhoneNumber.LostFocus

    If Not IsPhoneNumberValid(txtPhoneNumber.Text) Then  
        MessageBox.Show("Please Enter Phone Number!")
        txtPhoneNumber.Clear() 
        txtPhoneNumber.Focus() 
    End If
End Sub

```