JT4U
JT4U

Reputation: 620

How to validate a null value in a maskedtextbox phone number input mask

I want to check for null values. using this code below. i am still getting values in the textbox. The values i am getting in the textbox are "( ) -"...

If Text_Phone.Text IsNot "" Then
If BuildSqlFlag = True Then
BuildSql = BuildSql & " AND " & "Phone = " & Text_Phone.Text
Else
BuildSql = "Phone = " & Text_Phone.Text
End If
BuildSqlFlag = True
End If

i am not exactly sure what is needed from my code to be changed to i even tried the following:

If Text_Phone.Text IsNot "(   )   -" Then

But that was no help.

Upvotes: 4

Views: 1540

Answers (2)

langstrom
langstrom

Reputation: 1702

Set the TextMaskFormat to exclude prompt and literals.

Text_Phone.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals

MaskFormat Enumeration

Then when you do Text_Phone.Text it will be equal to "" if it's empty.

Upvotes: 1

Jande
Jande

Reputation: 1705

'Validate phone number in this format: 999-999-9999

Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles Button1.Click
        Dim phoneNumber As New Regex("\d{3}-\d{3}-\d{4}")
        If phoneNumber.IsMatch(TextBox1.Text) Then
            TextBox2.Text = "Valid phone number"
        Else
            TextBox2.Text = "Not Valid phone number"
        End If
    End Sub
End Class

'Validate phone number in this format (999)999-9999

Private Sub Button1_Click_1(sender As System.Object, _
 e As System.EventArgs) Handles Button1.Click
        Dim phoneNumber As New Regex("\(\d{3}\)\d{3}-\d{4}")
        If phoneNumber.IsMatch(TextBox1.Text) Then
            TextBox2.Text = "Valid phone number"
        Else
            TextBox2.Text = "Not Valid phone number"
        End If
End Sub

Upvotes: 1

Related Questions