Jump to content

eregi not working


Praetorian

Recommended Posts

I can't figure out why this isn't working. If I constructed the string correctly, it should produce an error if there isn't a .com/net/ something at the end of the email. And yet, it lets something@nowhere through without an error.

if (strlen(trim($Email)) < 1 || !eregi("^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$", $Email)) {					$errors[] = '<h3>Please Enter a Valid Email Address.</h3>';

Link to comment
Share on other sites

Did you borrow this? I don't really care. Just that I've seen a lot of these in the last few days, and this one is designed to let any address pass if it ends in dot WHATEVER as long as WHATEVER is an alpha string between 2 and 6 characters long. (Some of the newer domains have up to 6 chars.)Small price to pay?You could google the idea. There are some patterns out there that actually specify all the possible domains. You'll get better validation that way.

Link to comment
Share on other sites

That's the thing though. It doesn't work that way. It only produces an error when I try to submit "something" or "something@" but if I type "something@something" (notice no dot) it produces no error. Obviously the string is flawed somehow... but I can't for the life I me see how.And yes, I copied this, but I can't recall from where. I think I got it from the tutorial at the top of this forum... I just took out the / symbol at the beginning and the /xs at the end. Not sure what they were for but it didn't seem to change how the string performed.

Link to comment
Share on other sites

u use ^ as start and $ as end, but between those u have another $-sign, so it wil think it ends there already, try escaping those with \$

Link to comment
Share on other sites

FWIW, this script has been tested and does what you want. It's based on yours, so I won't vouch for its ultimate success. It still has the limitation I mentioned. But it doesn't tolerate the malformations you specifically complained about, and it does let pass pretty basic addresses as the popped into my head. Note the shift to from eregi to preg_match. I have no idea if that contributed. The use of GET query lets you test a lot of different addresses very quickly.

<?php   if (!isset($_GET['email']) || empty($_GET['email']) ) {	echo "Please add a query string in the form of '?email=address'";	   exit;   }   $a = $_GET['email'];   $p = "/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/i";   if (preg_match($p, $a) ){	echo "Match!";   } else {	echo "No match.";}?>

Link to comment
Share on other sites

Thanks both of you. The problem was where Wander said it was. Needed to escape the starting/ending symbols that I was allowing in the string.But I may steal your string and use it for eregi, Deirdre's Dad. Though... why does your string start with / and end with /i ?EDIT: The string you gave me didn't work when inserted into an eregi. It marked this@this.com as an invalid email.

Link to comment
Share on other sites

If you use what I gave you in place of eregi (like plug and play) it will work. Preg and Ereg are diferent ways of handling regular expressions. Preg is Perl style, Ereg is Posix extended. Perl uses the slashes. So of course you couldn't just take that part and substitute it. Like giving your car another steering wheel.Anyway, the manual says go with Preg routines over Ereg when you can.Why do you think I'd go to all that trouble and say I'd tested it if it didn't work?wink.gif

Link to comment
Share on other sites

Huh. Okay. Didn't know that. Sorry. I'll use that method then. Thanks again.EDIT:What does the empty function do exactly? Is it another way of checking if the variable has been set, or does it do the same as this line would...?Thanks for the info.

if (strlen(trim($Username)) < 1) echo "it's empty";

Link to comment
Share on other sites

Okay. If anyone else stumbles across this topic and wants to use it, I did some studying of the PERL style and managed to condense what Deirdre's Dad gave me into something shorter without losing functionality. Notice I also used # and # to mark the beginning and end of the string. You can use any non-alpha-numeric symbols as a delimiter. I used the # because there are less of them to escape.I tested this string and it does exactly what the previous string does, so long as you're using preg_match.I do have a question for anyone who comes across this. When using preg_match, do I need to escape all the characters I escaped? ( . - and the . outside of the []?) Thanks. And if anyone sees anything I could do to improve this please let me know.

#[\A\w]+[\w\.]*[\W]*@[\w]{1,61}[\w\.\-]*\.[\w]{2,6}$#

Link to comment
Share on other sites

You need to escape any character that has a special meaning. Characters like . and [] have special meanings, so if you want to match those specific characters instead of using their special meanings then you need to escape them.For your question about the empty function, check the reference. There is a list of things considered to be empty.http://www.php.net/manual/en/function.empty.php

Link to comment
Share on other sites

invoking empty() with a non-existant variable will trigger a notice. Its better to use isset() first.

Link to comment
Share on other sites

:) You keep talking about this string I gave you. It was your string all along. I just tweaked the ends so it would work in preg_match.If you want to try others, go here. They have a whole bunch. Some look very good.FWIW, I found the site by Googling email regex . Not so very hard to do.
Link to comment
Share on other sites

invoking empty() with a non-existant variable will trigger a notice. Its better to use isset() first.
That's what I thought too, but it turns out that's not the case.
empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
Link to comment
Share on other sites

Also, if OP is still playing with the email validation, this is what I use:

function validate_email($str){	return preg_match("/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix",$str);}

It might look like someone had an epileptic seizure on the keyboard, but it seems to work. You can also use this if the server supports it:http://www.php.net/manual/en/function.filter-var.php

Link to comment
Share on other sites

Right. That's where I originally got the one I posted. heh. From your tutorial.After studying the Perl method though from a book I have, I realized that the string I cut it down to does the same thing as your string.For instance.. \A denotes the beginning of the line (so ^ is not necessary.) \w is all of these symbols a-zA-Z0-9 so putting \w\d is unecessary since \w covers digits already. You only need \d if you only want digits. \W is all digits except a-zA-Z0-9 so you don't have to fill in all the $#^%&* etc.I also edited out the $ at the end because \z does the same thing (denoting that the preceding group should be at the end of the string).And on a side note, the x at the end removes whitespace and ignores comment symbols. So you only need it if you plan to break the line down into several lines with comments on what each block does.

Link to comment
Share on other sites

Okay. As I was typing my problem I figured out the solution. Here's my ammended string. The previous string was allowing multiple @ symbols. So I changed the \W to ^@ which will allow all symbols except the @ sign. So now it will mark more than one @ as an error, plus still allows all the characters \W would have allowed. Also, the \A should be before the character class to indicate that it should start with that character class.

#\A[\w]+[\w]*[^@]*@[\w]{1,61}[\.\-]*[\w]*\.[\w]{2,6}\z#

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...