Validating user's email address input from form

Kapusta
Kapusta used Ask the Experts™
on
#2: How can I detect if an email address entered on a form contains a "@" symbol, and at least one period "."?  I am trying to perform an error check on the input.  I know a little CGI scripting, so I am not asking for a complete script...just a "if...then" snippet would do fine.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Which language please?
Ok, its Perl.

Commented:
If you are talking perl -

if ($email =~ /\@.+\./ {
# "good"
} else {
# "bad"
}

That does what you ask - checks for a @ and a . after the @, but that really doesn't validate the email address. Look at http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html#How_do_I_check_a_valid_email_ad for a discussion and a pointer to a script that will do a better job.

If this answers your question, let me know and I will repost it as an answer. Good luck!

Commented:
oops, that line was supposed to be

if ($email =~ /\@.+\./) {

(forgot a ")")
Commented:
Just to restate this as an answer, the following does what you asked for.

if ($email =~ /\@.+\./) {
 # "good"
} else {
 # "bad"
}

But see http://www.perl.com/CPAN-local/doc/FAQs/FAQ/PerlFAQ.html#How_do_I_check_a_valid_email_ad for info on why this might not be what you really want to do.

good luck!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial