Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating email and url

Status
Not open for further replies.

PhoenixDown

Programmer
Jul 2, 2001
279
CA
I don't know if these are wrong. If they are, please fix it.

if (($in{'Field3'} eq "") || ($in{'Field3'} !~ /[\@|\.||a-zA-Z0-9]/)) {
&GBStandardHTML("You have entered an invalid email address. Your email address must contain an @, a period (.), and at least one letter or number. ");
exit;
}
}

How can I make the form know if there is an @, ., and at least one letter or number?

if ($vars_gen{'Field3Data'} eq "url") {
if (($in{'Field3'} eq "") || ($in{'Field3'} !~ /[http:\/\/|\.|a-zA-Z0-9]/)) {
&GBStandardHTML("You have entered an invalid URL. Your URL must include the exit;
}
}

How can I make the form know if there is http://, ., and at least one letter or number?

Thank you! :) - Go there!
 
while there might be an infinite number of approaches
to trying to validate an email address, according
to Jeffery Friedl in 'Mastering Regular Expressions',
it is, in fact, impossible(something about nested
comments being legel in email addresses, according
to the standard, page 294). That having been said,
we can make a crude ruff stab at it that will do a
lot, if not everything.

$email = 'me@nowhere.com';
if ($email =~ /\w+@\w+\.\w+/) { print "Matched $&\n"; }
| | | | |
| | | | multiple word chars [a-zA-Z0-9]
| | | an escaped dot \.
| | multiple word chars
| an at sign @
multiple word chars

This is grossly oversimplified, but, it might do enough
of what you want. Mr. Fiedl gives an example of a
functional regex for an email address assuming only one
level of nested comments in his book and it is 2/3 of
a page in a fairly small font. Really ugly.

about the
$url = 'if ($url =~ /http:\/\/.*?\.\w+/) { print "Matched $&\n"; }

HTH


keep the rudder amid ship and beware the odd typo
 
Fortunately, I've never seen anyone enter their email address with nested comments. However, I have seen email addresses with hyphens and extra periods in them, and I don't think \w will match either one (it says "alphanumeric plus "_"). This might work better:

/\A[\w-.]+@[\w-.]+?\.[\w]+\Z/

Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdryden, yours did not work. :-(

Here's what I currently have:

if ($vars_gen{'Field3Data'} eq "url") {
if (($in{'Field3'} eq "") || ($in{'Field3'} !~ /http:\/\/.*?\.\w+/)) {
&GBStandardHTML("You have entered an invalid URL. Your URL must include the exit;
}
}

I was wondering if you may make it so that the form will work if no data is entered, but if somthing is, it sees if it has http://?

Thanks!! - Go there!
 
I always use;

unless ($email =~ /.+\@.+\..+/) { &bad_email; }

Andy
 
For checking that url's have ' in front of them I usually just use this:
Code:
$url = "[URL unfurl="true"]http://".$url[/URL] unless $url =~ m[\Ahttp://];
That way they're not required to put in the ' I simply add it on if they didn't. It's a user-friendliness type of thing. (I'm very user-friendly :) ).

I'll check out the email regex and see what's wrong with it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I just tried out the email regex I posted above and it works fine. It will match "my-name.me\@some.other-company.com" as well as "tracy@bydisn.com".
Try this:
Code:
$email = "my-name.me\@some.other-company.com";
if ( $email =~ /\A[\w-.]+@[\w-.]+?\.[\w]+\Z/ ) {
	print "$email matched regex";
} else {
	print "$email did not match regex";
}
$email = "tracy\@bydisn.com";
if ( $email =~ /\A[\w-.]+@[\w-.]+?\.[\w]+\Z/ ) {
	print "$email matched regex";
} else {
	print "$email did not match regex";
}
It will print that both matched the regex.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top