Re: Validating textbox input
>'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
Re: Validating textbox input
>'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
Re: Validating textbox input
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
Re: Validating textbox input
change
Dim arrByteSequenceElements As String
to
Dim arrByteSequenceElements() As String
Re: Validating textbox input
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.
Re: Validating textbox input
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
Re: Validating textbox input
>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... ;-)
Re: Validating textbox input
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!!!!