4

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.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
JT4U
  • 620
  • 4
  • 18

2 Answers2

1

'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
Jande
  • 1,695
  • 2
  • 20
  • 32
  • i will use this on another input where i am using a textbox for a phone number. Thank you this was useful gave it an upvote. Please give this question an upvote. these two answer will help future visitors. thanks – JT4U Jun 06 '16 at 18:42
1

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.

langstrom
  • 1,652
  • 11
  • 14
  • Thank you so much. never knew maskformat can exclude literals. please give an upvote to future visitors as i know many people will use masktextboxes instead of textboxes for phone number validation. – JT4U Jun 06 '16 at 18:41