Results 1 to 7 of 7

Thread: [RESOLVED] Validating a textbox

  1. #1

    Thread Starter
    Member nn77's Avatar
    Join Date
    Dec 2005
    Posts
    45

    Resolved [RESOLVED] Validating a textbox

    How to validate a textbox on Lost Focus event so that only alphabetic characers are possible?of eq if the field is Name

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Validating a textbox

    U can use this code in the keypress event

    Copy the following code in the keypress event

    dim strvalid as string
    strvalid="abcdedfghijkilmnopqrstuvwxyzABCEDEFGHIJKILMNOPQRSTUVWXYZ"

    If InStr(strvalid, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    End If

    Hopes it helps u

  3. #3
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: Validating a textbox

    First you could catch the key pressed in the keypress event and if its a number then cancel it but that still doesnt stop them pasting a number in so try looping through the textbox in the validate event
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.  Dim i As Integer
    3.  
    4.    For i = 1 To Len(Text1.Text)
    5.     If IsNumeric(Mid$(Text1.Text, i, 1)) Then
    6.      MsgBox "No Numbers Please"
    7.      Cancel = True
    8.      Exit For
    9.     End If
    10.    Next i
    11.    
    12. End Sub

    casey.

  4. #4

    Thread Starter
    Member nn77's Avatar
    Join Date
    Dec 2005
    Posts
    45

    Re: Validating a textbox

    Thanx,that was helpful, but how to ignore values like ';./,]{}_+| and have an error message come up if these values are input?

  5. #5

    Thread Starter
    Member nn77's Avatar
    Join Date
    Dec 2005
    Posts
    45

    Re: Validating a textbox

    Help,Anyone,please...???

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Validating a textbox

    Check each Ascii character.

    VB Code:
    1. Dim i As Integer
    2.  
    3.     For i = 1 To Len(Text1.Text)
    4.         Select Case Asc(Mid$(Text1.Text, i, 1))
    5.             Case 65 To 90 'A - Z
    6.             Case 97 To 122 'a - z
    7.             Case Else
    8.                 MsgBox "Invalid Character " & Mid$(Text1.Text, i, 1)
    9.                 Cancel = True
    10.                 Exit For
    11.         End Select
    12.     Next

  7. #7

    Thread Starter
    Member nn77's Avatar
    Join Date
    Dec 2005
    Posts
    45

    Re: Validating a textbox

    Wow,thank you soo much!!!!!

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