36 Validation using Error Provider in VB.NET

Regular Expression:

^[789]\d{9}$


^     #Match the beginning of the string
[789] #Match a 7, 8 or 9
\d    #Match a digit (0-9 and anything else that is a "digit" in the regex engine)
{9}   #Repeat the previous "\d" 9 times (9 digits)
$     #Match the end of the string

Validating Phone Number Code:

Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim regex As Regex = New Regex("^[789]\d{9}$")

        Dim isValid As Boolean = regex.IsMatch(TextBox1.Text)

        If isValid Then
            ErrorProvider1.SetError(TextBox1, "Valid")
        Else
            ErrorProvider1.SetError(TextBox1, "InValid")
        End If
    End Sub
End Class





Ouput:






Previous
Next Post »