如何验证maskedtextbox电话号码输入掩码中的空值

时间:2016-06-06 18:06:46

标签: vb.net

我想检查空值。使用下面的代码。我仍然在文本框中获取值。我在文本框中得到的值是“() - ”......

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

我不确定我的代码需要什么才能更改为我甚至尝试了以下内容:

If Text_Phone.Text IsNot "(   )   -" Then

但这没有任何帮助。

2 个答案:

答案 0 :(得分:1)

'以此格式验证电话号码: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

'以此格式验证电话号码(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

答案 1 :(得分:1)

设置TextMaskFormat以排除提示和文字。

Text_Phone.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals

MaskFormat Enumeration

然后当你Text_Phone.Text时,如果它是空的,它将等于""