Results 1 to 20 of 20

Thread: Validation?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21

    Validation?

    Hello i was wondering if someone could please help me out?

    I need to validate some text boxes in my vb.net program so when a user inputs the wrong data a display message is shown telling them what they need to enter.

    can anyone tell me what code i would need to perform this, i have 8 text boxes in total, 2 are for text and 6 for numbers!

    Thanks alot

    Dave

  2. #2
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Well, validation is a pretty huge subject because of all of the possibilities and different inputs that you could receive...

    Instead of validating the text... I would only let them enter what you want them to by using the Mask Edit (Masked Edit maybe?) control within VB...

    Using this control, you can put an "input filter" of sorts in place and only allow them to enter numbers (to a certain length even) or characters...


    If you really want to get down to the nit'n'gritty of it... I would do a loop through the char array (string) (yes, they are arrays) and check each of the chars or numbers against a set of chars or numbers that you want to allow...

    something like..
    VB Code:
    1. For i = 0 to string.length
    2.   If not string.char(i) > "a" and not string.char(i) < "z" ...  etc..  Then
    3.     return false
    4.   end if
    5. next i

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Humm thanks sounds qute complicated, think ill just try the input filter.

    what code would i need for a simple ext button?

    Thanks

  4. #4
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    an ext button? please explain
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  5. #5
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    It's not really complicated... Just might take a little time to implement correctly... that's all


    ~Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Originally posted by squirrelly1
    an ext button? please explain
    sorry that was meant to be an exit button

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Simple exit?

    Me.Close()


  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    lol ok thanks thats great!

  9. #9
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I use 'masked edit', but there are some little problems. I was not able to intercept keys on that control and there is not a 'readonly' property to use, like is for texboxes.
    I prefer to control input by code. Here is an example of how to permit only a numeric input by Keyboard (It doesn't cover input from Ctrl+V !).
    In Keypress event of your textbox:

    If Not Char.IsDigit(e.KeyChar) Then e.Handled = True

    That's all. Now it accepts only digits. You can do the same for letters:

    If Not Char.IsLetter(e.KeyChar) Then e.Handled = True

    and so on.....
    If you need to accept, for example: digit, control chars, a decimal separator like "," and a limit of maximum 2 decimal digits:

    VB Code:
    1. Private Sub testi(ByRef casella As TextBox, ByVal car As Char, ByRef bool As Boolean)
    2.         If (casella.Text.IndexOf(",") >= 0 And car = ",") Then bool = True
    3.         If (Char.IsDigit(car) Or car = "," Or Char.IsControl(car)) = False Then
    4.             bool = True
    5.         End If
    6.         If (casella.Text.IndexOf(",") > 0 And Char.IsControl(car) = False) Then
    7.             If casella.Text.Substring(casella.Text.IndexOf(",")).Length > 2 Then
    8.                 bool = True
    9.             End If
    10.         End If
    11.     End Sub

    You can call, always in keypress event, a routine like that above, as in this example:

    VB Code:
    1. Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
    2.                 Me.testi(txbEuro1, e.KeyChar, e.Handled)
    3.             End Sub

    You have to spend a little of time, but when you'll be more confident with this approach you'll really be able to do everything you need with your textboxes

    Good job
    Live long and prosper (Mr. Spock)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    i've tried that but im getting this error back

    'KeyChar' is not a member of 'System.EventArgs'.
    'Handled' is not a member of 'System.EventArgs'.

    the above example would work fine i just want basic validation just so that there is only text in one and numbers in the other?

    Thanks

    Dave

  11. #11
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Why don't you just check against the ascii values of each of the characters in the string??? It's really a simple loop to implement...

    Take a look at the System.Text.Ascii...whatever... object... It shouldn't take long to figure out how to use it...

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    yeh i would but the code alextyx seems to be pritty straight forward!

    just can't understand them errors!!!

  13. #13
    Member
    Join Date
    Mar 2004
    Posts
    39
    Do a search on the microsoft site for validatingtextboxes and there is an example class to do this. I modified this class to handle also Combo Boxes and found it very good.

  14. #14
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear friend, I think you missed the right event. You must put the code in KeyPress event routine, not KeyDown or KeyUP:

    This first version permits digit only, the second also control chars (you need them, I think!)
    VB Code:
    1. Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
    2.        
    3.         If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
    4.        
    5.     End Sub
    6.  
    7.  
    8.    Private Sub txbEuro1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txbEuro1.KeyPress
    9.  
    10.                If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
    11.  
    12.     End Sub

    Try once again
    I'm waiting for your positive answer
    Live long and prosper (Mr. Spock)

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    sorry Firend, what is KeyPress how do i create this??

  16. #16
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    It's an event.

    You don't create it.

    It's already there for anything that you could think about pushing a key on.

    In your coding window, find the control in the left hand combo box and then find "KeyPress" in the right hand list.



    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    yeah sorted that now thanks.

    tried both bits of code on each the

    If Not Char.IsDigit(e.KeyChar) Then e.Handled = True

    works fine for entering numbers.

    i can't get the other one working just seems the same won't let me enter any text only numbers?

    If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True

    ????

    Thanks
    Last edited by VBdotnet_newbie; May 12th, 2004 at 06:30 PM.

  18. #18
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    If you want to enter letters use Char.IsLetter(e.KeyChar)

  19. #19
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Robymix was right! The second example I gave to you, it's useful because you can use some control keys! Good job
    Live long and prosper (Mr. Spock)

  20. #20

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    do u know if i could use this code

    VB Code:
    1. If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True

    and limit the field size so only 2 digits can be entered?

    Thanks

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