Validate text box entry

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
If I have a text box that I only want the values A, B, C, D or F entered into how can I validate this and in what event of the text box should I be using? I tried

[CODE ]
If Me.txtMidterm.Text <> "A" Or Me.txtMidterm.Text <> "B" Or Me.txtMidterm.Text <> "C" Or Me.txtMidterm.Text <> "D" Or Me.txtMidterm.Text <> "F" Then
MessageBox.Show("Please enter either A, B, C, D, or F")
Me.txtMidterm.Clear()
Me.txtMidterm.Focus()
End If
[/CODE]
 
Do it in the key down event. Create an array of allowable characters and on the keydown check if what was pressed is in the array, if it isn't set e.handled = true
 
Place the following line in the textbox's KeyPress event. Note that only the uppercase letters from A to F will be accepted.


If e.KeyChar < "A" Or e.KeyChar > "F" Then e.Handled = True


If you want both upper and lowercase letters from a-f and A-F to be accepted, then use this code instead:

If e.KeyChar < "A" Or e.KeyChar > "F" And e.KeyChar < "a" Or e.KeyChar > "f" Then
e.Handled = True
End If
 
Last edited:
Back
Top