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

    Validating textbox input

    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



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

    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



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

    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






  4. #4
    Join Date
    May 2000
    Location
    Scotland
    Posts
    42

    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


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

    Re: Validating textbox input

    change
    Dim arrByteSequenceElements As String

    to
    Dim arrByteSequenceElements() As String



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

    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.


  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    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

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

    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... ;-)


  9. #9
    Join Date
    May 2000
    Location
    Scotland
    Posts
    42

    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!!!!


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