|
-
Dec 21st, 2006, 08:07 AM
#1
Thread Starter
Hyperactive Member
[2005] Check field for Str Value
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
-
Dec 21st, 2006, 08:15 AM
#2
Addicted Member
Re: [2005] Check field for Str Value
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
The universal aptitude for ineptitude makes any human accomplishment an incredible miracle -Col. John P. Stapp
Please rate the posts that have helped. Makes us feel all warm and fuzzy
-
Dec 21st, 2006, 08:40 AM
#3
Re: [2005] Check field for Str Value
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
-
Dec 21st, 2006, 08:55 AM
#4
Thread Starter
Hyperactive Member
Re: [2005] Check field for Str Value
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
-
Dec 21st, 2006, 01:49 PM
#5
Re: [2005] Check field for Str Value
 Originally Posted by drawlings
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 spaces
I want to do a check to see if it contains any characters but I want to allow spaces
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|