CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2006
    Posts
    54

    Smile input validation

    hello all,

    Does anyone know a better way or function to retrict user input, for example special characters are not allow

    thanks

  2. #2
    Join Date
    Oct 2006
    Location
    slavia
    Posts
    42

    Re: input validation

    if u mean in textbox
    have u try this ...
    i validate a textbox in vb6 where user only can input character, space, and backspace.
    u can customize it so that number is also valid for the textbox

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not ((KeyAscii > Asc("A") - 1 And KeyAscii < Asc("Z") + 1) _
            Or (KeyAscii > Asc("a") - 1 And KeyAscii < Asc("z") - 1) _
            Or KeyAscii = 32 Or KeyAscii = 8 _
            ) _
        Then _
                
                '32 is for space, 8 is for backspace
                
                
                KeyAscii = vbKeyEscape
        End If
    End Sub
    it works like this :
    first u set the range of valid ascii ..
    if the ascii of the string/char that u want to check is not in the valid range,
    1. u can delete it,
    2. u can pop a messagebox to remind user that it is not a valid input

    with that code above, i change into vbKeyEscape, because the validation is processed on keypress even.

    may this give an explanation

  3. #3
    Join Date
    Jul 2006
    Posts
    54

    Talking Re: input validation

    ya, i get it already , thanks

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: input validation

    Quote Originally Posted by limck
    hello all,

    Does anyone know a better way or function to retrict user input, for example special characters are not allow

    thanks
    Is there any special reason for blocking the keys. If you are worried about the database queries failing because of these special characters then instead of blocking them, use Parameterized queries. Search the forum for simple code snippets on that.

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