CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2008
    Posts
    19

    Validating a text box

    hi! i want to make validation on a text box so that the first character is only A, B, C, or X..

    Code:
    sCustomerAccountNumber = txtCustomerAccountNumber.text
    
    If sCustomerAccountNumber.Chars(0) <> Chr(65) Then
                sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
                bErrorOccurred = True
            ElseIf sCustomerAccountNumber.Chars(0) <> Chr(66) Then
                sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
                bErrorOccurred = True
            ElseIf sCustomerAccountNumber.Chars(0) <> Chr(67) Then
                sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
                bErrorOccurred = True
            ElseIf sCustomerAccountNumber.Chars(0) <> Chr(88) Then
                sErrorMessage = sErrorMessage & "Customer Account Number should start with a letter!" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
                bErrorOccurred = True
    End If
    what happens with these code is whenever i try to put in A, B, C, or X.. i get the error message.. should i convert this into a loop..??

  2. #2
    Join Date
    May 2008
    Posts
    19

    Re: Validating a text box

    can someone close this..?? already found out how to do it..

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Validating a text box

    Click on Thread Tools, and then Mark as RESOLVED. Only you can close it out.

    Also, it'd help the next person to post the answer to any question.

    Thanks
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Validating a text box

    You should also look at Regular Expressions. You will have to write less code and it will be easier to maintain and understand.

  5. #5
    Join Date
    May 2008
    Posts
    19

    Re: Validating a text box

    what's Regular Expressions..??

    this is what i did:

    Code:
    sChar1 = Mid(txtCustomerAccountNumber.text, 1, 1)
    
    If sChar1 <> "A" And sChar1 <> "B" And sChar1 <> "C" And sChar1 <> "X" Then
                sErrorMessage = sErrorMessage & "Customer Acount Number should start with A, B, C, or X" & vbCrLf & "e.g. A, B, C or X" & vbCrLf & "e.g. A1234567" & vbCrLf
                bErrorOccurred = True
    but it didn't work properly... any suggestions..??

  6. #6
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: Validating a text box

    Use a regular expression such as Shuja Ali suggests to make the validation logic.

    You can take a look at it here:
    http://msdn.microsoft.com/en-us/library/hs600312.aspx

    It is a powerful tool and well worth the time to get introduced to.

  7. #7
    Join Date
    May 2008
    Posts
    224

    Re: Validating a text box

    Normally I just use an instr statement for such checks.

    Code:
    Dim ValidChars as String="ABCX"
     
    if instr(ValidChars,Mid(txtCustomerAccountNumber.text, 1, 1))=0 then
     
    ' do error stuff here
    end if
    Last edited by WillAtwell; May 21st, 2008 at 02:45 AM.

  8. #8
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Validating a text box

    Quote Originally Posted by manil
    hi! i want to make validation on a text box so that the first character is only A, B, C, or X..
    ...
    Code:
    		Select Case sCustomerAccountNumber.Chars(0)
    			Case "A", "B", "C", "X"
    				'Is ok
    			Case Else
    				'Is Wrong
    				sErrorMessage &= "Customer Account Number should start with a letter!" & vbCrLf & _
    								 "e.g. A, B, C or X" & vbCrLf & _
    								 "e.g. A1234567" & vbCrLf
    				bErrorOccurred = True
    		End Select

  9. #9
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Validating a text box

    How about a custom user control that takes a regular expression as a property and an isvalid property that gets set when valid - you could include an error provider in the control.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

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

Featured