Results 1 to 23 of 23

Thread: validation(resolved)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Thumbs up validation(resolved)

    hi,

    I want to do a validation on a textbox. it only accept numeric from -20 to 40.
    below is part of my coding but i cant do the validation on the length and only accept -20 to 40. Please help.

    Public Sub Val_Temp(TextObj As TextBox)

    Dim i As Integer, n As Integer
    Dim s As String, Char As Byte

    i = TextObj.SelStart

    For n = 1 To Len(TextObj)
    Char = Asc(Mid(TextObj, n, 1))

    If ((Char >= 48 And Char <= 57) Or (Char = 45 And n = 1) Then
    s = s & Mid(TextObj, n, 1)
    Else
    i = i - 1
    End If
    Next n

    If i > Len(s) Or i < 0 Then i = Len(s)

    TextObj.Text = s
    TextObj.SelStart = i
    End Sub
    Last edited by vivian2u; Apr 21st, 2005 at 11:33 AM.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: validation

    How about this?
    VB Code:
    1. If TextObj < -20 Or TextObj > 40 Then
    2.     MsgBox "Invalid"
    3. End If
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    it cant work here because i m using

    Private Sub text1_Change()
    Val_Temp text1
    end sub

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: validation

    how cant it work..?

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: validation

    VB Code:
    1. If Len(Abs(TextObject)) = 2 Then
    2.     If TextObj < -20 Or TextObj > 40 Then
    3.         MsgBox "Invalid"
    4.     End If
    5. Else
    6.     MsgBox "Invalid"
    7. End If
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    ABs cant accept "-"

    if Len(Abs(TextObject)) = 2 Then

  7. #7
    Lively Member
    Join Date
    Apr 2005
    Posts
    68

    Re: validation

    ABS accept all numerics, positive and negative, and return the absoloute value (disregards the negative sign). You may have to use Val(text) to convert from string to integer.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    i got compile error ..variable required - can't assign to this expression when i use Abs

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    Not sure if I'd do it this way, but to make it work, you need this:

    VB Code:
    1. If Len(Abs(val(TextObject.text))) = 2 Then
    2.     If val(TextObj.text) < -20 Or val(TextObj.text) > 40 Then
    3.         MsgBox "Invalid"
    4.     End If
    5. Else
    6.     MsgBox "Invalid"
    7. End If
    Last edited by dglienna; Apr 19th, 2005 at 10:13 PM.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    it cant work.

    i want a textbox validation buy using Private Sub text1_Change()

    (a) it only accept numeric from -20 to 40.
    (b) the "-" only allow in front
    (c) if negative numeric , allow the length to 3 and not allow the user to press the 4th digit
    (d) if posivive numeric, allow the length to 2 and not allow the user press the 3th digit.

    my coding below is only work on (b)
    Public Sub Val_Temp(TextObj As TextBox)

    Dim i As Integer, n As Integer
    Dim s As String, Char As Byte

    i = TextObj.SelStart

    For n = 1 To Len(TextObj)
    Char = Asc(Mid(TextObj, n, 1))

    If ((Char >= 48 And Char <= 57) Or (Char = 45 And n = 1) Then
    s = s & Mid(TextObj, n, 1)
    Else
    i = i - 1
    End If
    Next n

    If i > Len(s) Or i < 0 Then i = Len(s)

    TextObj.Text = s
    TextObj.SelStart = i
    End Sub

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    My routine won't work in Text_Change(), but it could be in Text_Validate(), or even Text_LostFocus()

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    but i want to have the validation in Text_Change(), any idea??

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    You could check

    1) for numerics and no decimal points
    2) the '-' sign only at position 1
    3) no more than 2 characters, unless #2 is true

    it will fire each time something is put into or taken out of the textbox

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    can someone give me the coding in :

    3) no more than 2 characters, unless #2 is true

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    There are too many combinations for the change event. I've tried 3 different approaches, and each one crashed.

    Why don't you want to use the keypress event? or the lost_focus event?

    It would be much cleaner to code.

    The change event fires when you hit backspace, or delete, and the length can be 0, but you can't use 0 in the mid statement. you can't compare the length to the previous instance either.
    Last edited by dglienna; Apr 20th, 2005 at 12:02 AM.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    i thinking of the keypress even now... you are right...

    hows the coding in keypress even ?

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    Here is a start. I wasted too much time on the other mess trying to make it work. I'll look at it again tomorrow if you can't figure it out.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Text1.Text = ""
    5. End Sub
    6.  
    7. Private Sub Text1_KeyPress(KeyAscii As Integer)
    8.   If KeyAscii < 49 Or KeyAscii > 57 Then
    9.     KeyAscii = 0
    10.   Else
    11.     If KeyAscii = 45 And Text1.Text <> "" Then
    12.       KeyAscii = 0
    13.     End If
    14.   End If
    15. End Sub

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    cant work...its not accept '-', because of

    If KeyAscii < 49 Or KeyAscii > 57 Then
    KeyAscii = 0

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: validation

    keyascii = 45 is the '-'
    i took that one out.

  20. #20
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: validation

    Give this a go.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim IsNegative As Boolean
    4.  
    5. Private Sub Text1_Change()
    6.  
    7.     If Left$(Text1.Text, 1) = "-" Then
    8.         Text1.MaxLength = 3
    9.         IsNegative = True
    10.     Else
    11.         Text1.MaxLength = 2
    12.         IsNegative = False
    13.     End If
    14.    
    15. End Sub
    16.  
    17. Private Sub Text1_KeyPress(KeyAscii As Integer)
    18.  
    19.     Select Case KeyAscii
    20.         Case vbKey0 To vbKey9                   ' These keys are OK
    21.             If Len(Text1.Text) = 1 Then
    22.                 If Not IsNegative Then
    23.                     If Val(Text1.Text) >= 4 Then
    24.                         If KeyAscii > vbKey0 Then
    25.                             KeyAscii = 0
    26.                         End If
    27.                     End If
    28.                 End If
    29.             Else
    30.                 If Abs(Val(Text1.Text)) >= 2 Then
    31.                     If KeyAscii > vbKey0 Then
    32.                         KeyAscii = 0
    33.                     End If
    34.                 End If
    35.             End If
    36.         Case 45                                 ' 45 is '-'
    37.             If Len(Text1.Text) > 0 Then
    38.                 KeyAscii = 0
    39.             End If
    40.         Case vbKeyBack, vbKeyDelete             ' Allow backspace & delete
    41.         Case Else
    42.             KeyAscii = 0
    43.     End Select
    44.    
    45. End Sub
    Last edited by pnish; Apr 20th, 2005 at 01:31 AM.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    this is exactly what i want but when it is negative it accept '0' for
    -30 ,-40, -50, -60, -70, -80 and -90.

    how can i correct this errors.
    thanks

  22. #22
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: validation

    Did you try and figure out why it wasn't working properly? I've amended the code, this should do what you want.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim IsNegative As Boolean
    4.  
    5. Private Sub Text1_Change()
    6.  
    7.     If Left$(Text1.Text, 1) = "-" Then
    8.         Text1.MaxLength = 3
    9.         IsNegative = True
    10.     Else
    11.         Text1.MaxLength = 2
    12.         IsNegative = False
    13.     End If
    14.    
    15. End Sub
    16.  
    17. Private Sub Text1_KeyPress(KeyAscii As Integer)
    18.  
    19.     Select Case KeyAscii
    20.         Case vbKey0 To vbKey9                   ' These keys are OK
    21.             If Len(Text1.Text) = 1 Then
    22.                 If Not IsNegative Then
    23.                     If Val(Text1.Text) > 4 Then
    24.                         KeyAscii = 0
    25.                     ElseIf Val(Text1.Text) = 4 Then
    26.                         If KeyAscii > vbKey0 Then
    27.                             KeyAscii = 0
    28.                         End If
    29.                     End If
    30.                 End If
    31.             Else
    32.                 If Abs(Val(Text1.Text)) > 2 Then
    33.                     KeyAscii = 0
    34.                 ElseIf Abs(Val(Text1.Text)) = 2 Then
    35.                     If KeyAscii > vbKey0 Then
    36.                         KeyAscii = 0
    37.                     End If
    38.                 End If
    39.             End If
    40.         Case 45                                 ' 45 is '-'
    41.             If Len(Text1.Text) > 0 Then
    42.                 KeyAscii = 0
    43.             End If
    44.         Case vbKeyBack, vbKeyDelete             ' Allow backspace & delete
    45.         Case Else
    46.             KeyAscii = 0
    47.     End Select
    48.    
    49. End Sub
    I know the code is sorely lacking in comments, so why don't you spend some time stepping through the code to try and understand how it works. You may surprise yourself and come up with a better way to do it.

    BTW There is still a subtle logic error in the code. See if you can figure out what it is and then fix it.
    Last edited by pnish; Apr 20th, 2005 at 05:53 PM.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: validation

    yes, this is exactly what i want and it works perfectly. Thanks again

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