【问题标题】:How to validate a null value in a maskedtextbox phone number input mask如何验证掩码文本框电话号码输入掩码中的空值
【发布时间】:2023-03-20 10:29:01
【问题描述】:

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

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

但这无济于事。

【问题讨论】:

  • 请不要投反对票,因为如果需要我可以解决我的问题。请解释一下我需要改进的地方。
  • 首先删除不相关的标签,其中一些标签是互斥的。如果不确定,请悬停鼠标以阅读每个摘要
  • 我删除了两个标签。谢谢你的反馈。还有什么我应该解决的吗?
  • 不要连接字符串来构建 SQL 查询。使用参数
  • 有没有可以用来构建参数的示例?编程新手,但这些指南对我适应 vs. 有很大帮助。

标签: vb.net


【解决方案1】:

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

Text_Phone.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals

MaskFormat Enumeration

那么当你执行Text_Phone.Text 时,如果它为空,它将等于""

【讨论】:

  • 非常感谢。从来不知道 maskformat 可以排除文字。请给未来的访问者投票,因为我知道很多人会使用 masktextboxes 而不是 textboxes 来验证电话号码。
【解决方案2】:

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

【讨论】:

  • 我将在另一个输入中使用它,我正在使用文本框作为电话号码。谢谢,这很有用,给了它一个赞成票。请给这个问题一个赞成票。这两个答案将对未来的访客有所帮助。谢谢
猜你喜欢
  • 2017-08-25
  • 1970-01-01
  • 2020-06-06
  • 2016-10-14
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多