Click to See Complete Forum and Search --> : validation of a text field


LouLou
January 26th, 2000, 11:58 AM
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

Johnny101
January 26th, 2000, 03:00 PM
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

Johnny101
January 26th, 2000, 03:03 PM
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

Lothar Haensler
January 27th, 2000, 01:24 AM
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

LouLou
January 27th, 2000, 12:12 PM
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