Best Event for textbox validation?

B

BillyRogers

What event should be used to run code to validate a textbox?

I had this in the OnEnter event and it ran the code everytime the form was
opened before the user had a chance to enter anything.

Dim re, s

Set re = New RegExp
re.Global = True
s = txtEmailAddress.Text

re.Pattern =
"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

If Not re.Test(s) Then

MsgBox "Email address is NOT valid."

End If
--
Billy Rogers

Dallas,TX

Currently Using SQL Server 2000, Office 2000 and Office 2003
 
D

Dirk Goldgar

BillyRogers said:
What event should be used to run code to validate a textbox?

I had this in the OnEnter event and it ran the code everytime the form was
opened before the user had a chance to enter anything.

Dim re, s

Set re = New RegExp
re.Global = True
s = txtEmailAddress.Text

re.Pattern =
"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

If Not re.Test(s) Then

MsgBox "Email address is NOT valid."

End If
--
Billy Rogers

Dallas,TX

Currently Using SQL Server 2000, Office 2000 and Office 2003


I'd use the text box's BeforeUpdate event, if you want to be able to cancel
the update and force the user to enter a valid address; or its AfterUpdate
event if you just want to warn the user. Note, though, that neither of
those events will fire if the user doesn't enter anything in the text box,
nor for previously entered data.

You could, alternatively, use the form's BeforeUpdate event to keep a record
from being saved with an invalid e-mail ddress. That would defer the
validation until the record was about to be saved.

FWIW, I use a simpler function to validate e-mail addresses without
incurring the overhead of a RegExp. I'm not very good with regular
expressions, so it may not catch everything your pattern does, but it
catches a lot. This is the code I use:

'----- start of code -----
Function IsValidEmailAddress(Candidate As String) As Boolean

If Trim(Candidate) Like "?*@[!.]*.[!.]*" Then
If Not Candidate Like "*@*@*" Then
IsValidEmailAddress = True
End If
End If

End Function
'----- end of code -----
 
M

Marshall Barton

BillyRogers said:
What event should be used to run code to validate a textbox?

I had this in the OnEnter event and it ran the code everytime the form was
opened before the user had a chance to enter anything.

Dim re, s

Set re = New RegExp
re.Global = True
s = txtEmailAddress.Text

re.Pattern =
"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

If Not re.Test(s) Then

MsgBox "Email address is NOT valid."

End If


For an unbound text box, use its AfterUpdate event.

For bound controls where you can not allow an invalid entry,
you can use the text box's BeforeUpdate event with Cancel
and Undo. Or, if you want to avoid user irritation when
they don't know the valid entry, use the form's BeforeUpdate
event with Cancel.
 
J

John Nurick

BeforeUpdate is usually good.

What event should be used to run code to validate a textbox?

I had this in the OnEnter event and it ran the code everytime the form was
opened before the user had a chance to enter anything.

Dim re, s

Set re = New RegExp
re.Global = True
s = txtEmailAddress.Text

re.Pattern =
"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"

If Not re.Test(s) Then

MsgBox "Email address is NOT valid."

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top