Results 1 to 5 of 5

Thread: [2005] Check field for Str Value

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Question [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

  2. #2
    Addicted Member
    Join Date
    Dec 2006
    Location
    London, England
    Posts
    142

    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:
    1. Private Sub txtbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox1.KeyPress
    2.         'This is the enter key, but you can also keys.enter
    3.         If e.KeyChar = Chr(13) Then
    4.             'do what ever
    5.         end if
    6. 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

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Check field for Str Value

    You can use regex to check for a valid phone number
    VB Code:
    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim regexp As New Regex("^\(?\d{3}\)?[\s|-]?\d{3}[\s|-]?\d{4}$")
    6.  
    7.         If regexp.IsMatch(Me.TextBox1.Text) Then
    8.             MsgBox("Valid Phone Number")
    9.         Else
    10.             MsgBox("Invalid Phone Number")
    11.         End If
    12.     End Sub
    13. 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
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: [2005] Check field for Str Value

    Thanks for you suggestions

    I have done this instead and it seems fine

    VB Code:
    1. tmpStr = txFax1.Text
    2.             For i = 1 To tmpStr.Length - 1
    3.                 If Not IsNumeric(tmpStr.Chars(i)) Then
    4.                     msgbox "Fax_1 Number field contains a invalid character"
    5.                     Exit For
    6.                 End If
    7.             Next

    Thanks for help

    Cheers

    Dan

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Check field for Str Value

    Quote Originally Posted by drawlings
    Thanks for you suggestions

    I have done this instead and it seems fine

    VB Code:
    1. tmpStr = txFax1.Text
    2.             For i = 1 To tmpStr.Length - 1
    3.                 If Not IsNumeric(tmpStr.Chars(i)) Then
    4.                     msgbox "Fax_1 Number field contains a invalid character"
    5.                     Exit For
    6.                 End If
    7.             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
  •  



Click Here to Expand Forum to Full Width