CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 1999
    Posts
    158

    validation of a text field

    How would I set up a for loop to check to see that
    the text entered into a field if formatted correctly. I want the text to apear like this
    B8G 7GT, so that you would get a letter number letter, then a space, then number letter number. I am very new at VB and am not sure how to set it up.

    Thanks


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: validation of a text field

    You might be able to set up a masked edit box to hold that pattern for you, but i'm not sure if it can such a complex pattern. If not then you could something like this:

    Function ValidateText(txtBox as TextBox) as Boolean
    Dim i as Integer
    Dim bOkay as Boolean

    for i = 1 to 7 'there are seven spaces that you want to check
    If Not bOkay then Exit for 'if something was wrong, exit the loop
    Select Case i
    Case 2,5,7 ' the positions in the string that should be numbers
    If IsNumeric(mid(txtBox.Text,i,1)) then 'Check the format
    bOkay = true
    else
    bOkay = false
    End If
    Case 1,3,6 'the positions where they should be letters
    If Asc(Ucase(mid(txtBox.Text,i,1))) > 64 And _
    Asc(Ucase(mid(txtBox.Text,i,1))) < 91 then
    ' if the character is a letter of the alphabet.
    bOkay = true
    else
    bOkay = false
    End If
    Case 4 ' the space
    If Asc(mid(txtBox.Text,i,1) = 32 then 'if it's a space character
    bOkay = true
    else
    bOkay = false
    End If
    End Select
    next i

    ValidateText = bOkay
    End Function




    This should get you started.

    hope it helps,
    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: validation of a text field

    Ooops, i forgot to intialize the boolean variable. Place this line of code just before the For i = 0 to 7 line:

    bOkay = True

    This way, we assume that things are going to all right and when we enter the loop we'll get past that first check.

    Sorry,
    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: validation of a text field

    if you make use of the "Microsoft VBscript Regular Expression" TypeLib in VB 5 or 6 that comes with IE5 you can code like this:

    private Sub Text1_Validate(Cancel as Boolean)
    Dim strText as string
    strText = Text1.Text
    Dim r as new RegExp
    r.Pattern = "[A-Z][0-9][A-Z] [0-9][A-Z][A-Z]"
    If r.Test(strText) then
    else
    MsgBox "invalid data"
    End If
    End Sub





  5. #5
    Join Date
    Jun 1999
    Posts
    158

    Re: validation of a text field

    This is what I was looking for but the code is having trouble with the Asc, I am using it with ASP code so perhaps that is the problem.

    Louise


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