check characters in string

R

red6000

Hi I have a userform with a textbox where the user will enter an email
address.

What I would like to do is have some code that checks that the text entered
is a valid email address format (like website forms do).

so what I was thinking was having code that:

checked that there were no spaces (or other characters like / etc) in the
string
check that the string contained an @ character
and checked that after the @ character there is a .

Is anyone able to point me in the right direction?

Thanks.
 
D

Doug Robbins - Word MVP

You would use the Instr() function for this. If the item that you want to
check for is not present, it will return 0, otherwise it will return the
position of the item in the string.

You are obviously going to need to use the function multiple times, once for
each of the characters that cannot be used, once for the @ and again for the
.. after the @ which should return a number representing a position greater
than that of the @. You are going to have to deal with the possibility
however of there being a period before the @ as well.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

red6000

That works a treat for me.

Thank you.

Doug Robbins - Word MVP said:
You would use the Instr() function for this. If the item that you want to
check for is not present, it will return 0, otherwise it will return the
position of the item in the string.

You are obviously going to need to use the function multiple times, once
for each of the characters that cannot be used, once for the @ and again
for the . after the @ which should return a number representing a position
greater than that of the @. You are going to have to deal with the
possibility however of there being a period before the @ as well.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Karl E. Peterson

red6000 said:
Hi I have a userform with a textbox where the user will enter an email
address.

What I would like to do is have some code that checks that the text entered
is a valid email address format (like website forms do).

so what I was thinking was having code that:

checked that there were no spaces (or other characters like / etc) in the
string
check that the string contained an @ character
and checked that after the @ character there is a .

Is anyone able to point me in the right direction?

See section 3.4.1 here: http://www.faqs.org/rfcs/rfc2822.html

That is _the_ authoritative source for any algorithm that does this. To give you an
idea of the defensive practices you'll need to consider, here's a module that
implements the date/time formatting from the same RFC:

http://vb.mvps.org/samples/raw/MRfc2822.bas.txt
 

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