Click to See Complete Forum and Search --> : Validating textbox input


mazwinkel
June 16th, 2000, 07:56 AM
Hello - I am a beginner having problems with the validate event on a text box in a user control...can anyone give some advice...

Situation is.

Textbox must accept data in two formats:
Hex Sequence: 06H,05H,06H,FFH,FDH... (max value of an element = FFH)
Decimal Sequence:6,5,6,255,254... (max value of an element = 255)

There is also a variable read in at runtime which holds maximum number of either Hex or Dec elements in the text box.

I'm trying to write a validate event which checks that user has stuck to the rules above and am finding it quite tricky - any pointers or comments would be much appreciated...Heres what I've got so far...

Private Sub Text1_Validate()

Dim IsHexSequence As Boolean
Dim szSearchString As String
Dim arrByteSequenceElements As String
'Dim MaxNoOfElements As Integer

pSearchString = Text1.Text
'Check whether we are dealing with a hex or decimal sequence
If (InStr(1, szSearchString, "H") <> 0) Or (InStr(1, szSearchString, "h") <> 0) Then
IsHexSequence = True
End If

'Check that there are not more than maximum number of column separated elements
arrByteSequenceElements = Split(szSearchString, ",")
If (IsHexSequence) Then
'How can I check that number of elements doesn't exceed MaxNoOfElements?
'How can I check that the elements of the arrByteSequenceElements conform
'to the required hex sequence format of XXH,XXH,XXH?

End Sub

What can I say - if you have any ideas I'd love to hear them....thank you!
Regards,
Mairi

Lothar Haensler
June 16th, 2000, 08:03 AM
>'How can I check that number of elements doesn't exceed MaxNoOfElements?

arrByteSequenceElements = Split(szSearchString, ",")
if ubound(ArrByteS...) >= MaxNo then
msgbox "too many entries"
Cancel = true
exit sub
end if

Lothar Haensler
June 16th, 2000, 08:09 AM
>'How can I check that the elements of the arrByteSequenceElements conform
'to the required hex sequence format of XXH,XXH,XXH?

I'd loop over all elements of the array that you have created by calling the split function.
Add a Reference to the Microsoft VBSCript Regular Expressions component, then use the following sample to test for a hex digit:


Dim r as new RegExp
r.Pattern = "[0-9A-F][0-9A-F][hH]"
r.Test( yourString ) returns true if is a valid string

mazwinkel
June 16th, 2000, 08:52 AM
Hello - thanks for the excellent advice. I've never have guessed about that!!!!
I have a slight problem with my code below in that it is saying UBound is expecting an array. I can't see why it wouldn't have an array though. I've tried to debig what is happening and the value of text1.txt = "45H,45H"

Can you see what's wrong?

Private Sub Text1_Validate(Cancel As Boolean)

Dim IsHexSequence As Boolean
Dim szSearchString As String
Dim arrByteSequenceElements As String
Dim MaxNoOfElements As Integer
Dim i As Integer 'Array counter

szSearchString = Text1.Text

If (InStr(1, szSearchString, "H") <> 0) Or (InStr(1, szSearchString, "h") <> 0) Then
IsHexSequence = True
End If
arrByteSequenceElements = Split(szSearchString, ",")
If UBound(arrByteSequenceElements) > MaxNoOfElements Then

MsgBox "Too Many Entries in Hex Sequence"
Cancel = True
Exit Sub
End If

'Check that the elements of the arrByteSequenceElements conform
'to the required hex sequence format of XXH,XXH,XXH including pre-fixed zeros?
Dim r As New RegExp
r.Pattern = "[0-9A-F][0-9A-F][hH]"
r.Test (yourString) 'returns true if is a valid string


End Sub


Thanks,
Mairi

Lothar Haensler
June 16th, 2000, 08:54 AM
change
Dim arrByteSequenceElements As String

to
Dim arrByteSequenceElements() As String

Lothar Haensler
June 16th, 2000, 08:57 AM
maybe I should make the hex test more clear.
here is a sample

Dim teststring as string
teststring = "45H,0DH"
Dim r as new RegExp
r.Pattern = "[0-9A-F][0-9A-F][hH]"
Dim a() as string
a = Split(teststring, ",")
Dim i as Integer
for i = 0 to UBound(a)
If r.Test(a(i)) then
' ok
else
MsgBox "invalid entry: " & a(i)
End If
next i



...change it to your needs.

Chris Eastwood
June 16th, 2000, 08:58 AM
In addition to Lothars suggestion, I'd recommend a little more error checking, namely :


'
szSearchString = Text1.Text
'
If len(sSearchString) = 0 then
MsgBox "nothing Entered" ' or similar....
Exit Sub
End If
'




- otherwise, and empty string, will cause the ubound call to fail later on (or at least, you'll get some very strange results).


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Lothar Haensler
June 16th, 2000, 09:09 AM
>otherwise, and empty string, will cause the ubound call to fail later on (or at least, you'll get some very strange results).


error checking is always good.
Although, an empty string would not break the code I posted, because Ubound would return -1 and the for loop would terminate immediately... ;-)

mazwinkel
June 16th, 2000, 09:21 AM
Chris and Lothar - thank you so much for your help. The support,advice, hints you VB'ers are giving is terrific!

We're not worthy....we're not worthy!!!!