Hey Guys,
I have a textbox that I am using as a contact number field. I want to do a check to see if it contains any characters but I want to allow spaces
Cheers
Dan
Printable View
Hey Guys,
I have a textbox that I am using as a contact number field. I want to do a check to see if it contains any characters but I want to allow spaces
Cheers
Dan
Hi, you can use, txtbox1_keypress to catch what keys are been wrote in txtbox1, here's a little sample
VB Code:
Private Sub txtbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress 'This is the enter key, but you can also keys.enter If e.KeyChar = Chr(13) Then 'do what ever end if end sub
Redmo
You can use regex to check for a valid phone number
VB Code:
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 regexp As New Regex("^\(?\d{3}\)?[\s|-]?\d{3}[\s|-]?\d{4}$") If regexp.IsMatch(Me.TextBox1.Text) Then MsgBox("Valid Phone Number") Else MsgBox("Invalid Phone Number") End If End Sub End Class
This will allow phone numbers with spaces or dashes or no spaces and also allows Parentheses around the area code
If you only want to allow spaces, change it to
^\d{3}[\s]?\d{3}[\s]?\d{4}$ -This will allow a 10 digit number with or without spaces
Thanks for you suggestions
I have done this instead and it seems fine
VB Code:
tmpStr = txFax1.Text For i = 1 To tmpStr.Length - 1 If Not IsNumeric(tmpStr.Chars(i)) Then msgbox "Fax_1 Number field contains a invalid character" Exit For End If Next
Thanks for help
Cheers
Dan
This will work as long as the entered text doesn't contain anything but numbers... No spaces, no dash, no parenthesis... However, I noticed the requirement in your original post is to allow spacesQuote:
Originally Posted by drawlings
Quote:
I want to do a check to see if it contains any characters but I want to allow spaces