...

View Full Version : eregi, How to I change this...



Ludatha
04-25-2008, 04:35 PM
if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {

This is my current code, but it doesnt allow emails with a . before the @ symbol.
How can I change this to allow it.

This is NOT allowed:
adam.smells@bobo.com
chickin.smells.funny@gmail.com
etc.

This IS allowed:
adam@bob.com
hello@goodbye.co.uk
etc.

Note: The emails are just examples :P

Mwnciau
04-25-2008, 04:53 PM
if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-.]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {

tomws
04-25-2008, 04:54 PM
if (!eregi('^[a-z]+[a-z0-9._-]*@ ...


Just put the dot in there, like above.

Incidentally, the 'i' in eregi means 'case-insensitive', so you can get by without the range of caps, as illustrated above.

tomws
04-25-2008, 04:55 PM
if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-.]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {

That one will probably result in a range error because the dash is between two terms.

oesxyl
04-25-2008, 04:58 PM
if (!eregi('^[a-zA-Z]+[a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {



if (!eregi('^[a-zA-Z]+[\.a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {
add a \. in second []

This is a wrong way to validate email address, look into rfc822 for details.
For example me@you@is.valid pass this regex.

regards

Ludatha
04-25-2008, 05:19 PM
Thanks oesxyl, it works fine, if the email is not valid, they will not get an email so ill kep it as that :)

oesxyl
04-25-2008, 05:24 PM
I tried that before, but this is what I get:

Warning: eregi() [function.eregi]: REG_ERANGE in /home/ludatha/public_html/signup.php on line 38
Error!
The following error(s) occured:
- Please provide a valid email address.


if (!eregi('^[a-zA-Z]+[\.a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}', stripslashes(trim($_POST['email'])) )) {
That is line 38
this way, see also tomws post about range error.
Did you notice that me@you@is.valid pass the regex?

regards

tomws
04-25-2008, 05:30 PM
Did you notice that me@you@is.valid pass the regex?

That example fails your pattern, not passes, when I do this...

$pattern = '^[a-zA-Z]+[\.a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}';
$string = "me@you@is.valid";

if ( !eregi($pattern,$string) )
{
echo "no\n";

} // end: if
else //
{
echo "yes\n";

} // end: else


???

Incidentally, would it be wise to have a '$' at the end?

Iszak
04-25-2008, 05:38 PM
Ugh you are forgetting the most important thing. The best way to validate an email is to send an email to it and get them to check for it but if you want one of the best email regex here they are. http://fightingforalostcause.net/misc/2006/compare-email-regex.php

spaz
04-25-2008, 05:40 PM
This is a wrong way to validate email address, look into rfc822 for details.
For example me@you@is.valid pass this regex.

regards

That doesn't generate a match... anyway, here's a cleaner way to do this one:


if (!eregi('^\w+(\.|\w|-)*@(\w+)(\.\w{2,}\.?(\w{2,})?)', stripslashes(trim($_POST['email'])) )) {

\w = [a-zA-Z0-9_]

oesxyl
04-25-2008, 05:58 PM
That example fails your pattern, not passes, when I do this...

$pattern = '^[a-zA-Z]+[\.a-zA-Z0-9_-]*@([a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+){1,2}';
$string = "me@you@is.valid";

if ( !eregi($pattern,$string) )
{
echo "no\n";

} // end: if
else //
{
echo "yes\n";

} // end: else


???

Incidentally, would it be wise to have a '$' at the end?
it print a 'no' or a 'yes'?

I realise that when I said 'pass' was a little ambigue, but this regex will consider the email address I post as valid email that means don't work.

agree with you that a $ must be used, :).

regards

oesxyl
04-25-2008, 06:01 PM
Ugh you are forgetting the most important thing. The best way to validate an email is to send an email to it and get them to check for it but if you want one of the best email regex here they are. http://fightingforalostcause.net/misc/2006/compare-email-regex.php
you can't validate a email address using only regex.
agree with first, suggestion, :)


That doesn't generate a match... anyway, here's a cleaner way to do this one:


if (!eregi('^\w+(\.|\w|-)*@(\w+)(\.\w{2,}\.?(\w{2,})?)', stripslashes(trim($_POST['email'])) )) {

\w = [a-zA-Z0-9_]
did you test this?

regards

spaz
04-25-2008, 06:19 PM
did you test this?

regards

Only in EditPad Pro

oesxyl
04-25-2008, 06:22 PM
Only in EditPad Pro
for me don't work, was also my first attempt to make the regex more simple, :)

regards

tomws
04-25-2008, 06:23 PM
it print a 'no' or a 'yes'?


It prints 'no'.

spaz
04-25-2008, 06:24 PM
I just tried mine in a live environment and it did work. Maybe \w only works with PHP5 or something?

oesxyl
04-25-2008, 06:27 PM
It prints 'no'.
That means mail address not valid, I'm wrong?

regards

tomws
04-25-2008, 06:30 PM
That means mail address not valid, I'm wrong?

Correct. It means invalid.
!eregi($pattern,$string) Unless I haven't had enough coffee this morning...

oesxyl
04-25-2008, 06:33 PM
I just tried mine in a live environment and it did work. Maybe \w only works with PHP5 or something?
I have php 5, and I test in command line, that if I'm not wrong give same results as on the server. Maybe could be usefull somebody else test then ours.

regards

oesxyl
04-25-2008, 06:35 PM
Correct. It means invalid.
!eregi($pattern,$string) Unless I haven't had enough coffee this morning...
thank you, I was not sure that I don't mess something, :)

regards

tomws
04-25-2008, 06:43 PM
I have php 5, and I test in command line, that if I'm not wrong give same results as on the server. Maybe could be usefull somebody else test then ours.

Also doesn't work here. There's a comment on the php.net function page that says it won't work. No idea why. Anyway, I think the \w also includes spaces, so it might not be good anyway.

oesxyl
04-25-2008, 06:55 PM
I just tried mine in a live environment and it did work. Maybe \w only works with PHP5 or something?
probably is the enviroment, try another way.


Also doesn't work here. There's a comment on the php.net function page that says it won't work. No idea why.
thank you, I use pcre usualy, and this time I simply forget about manual.


Anyway, I think the \w also includes spaces, so it might not be good anyway.
I'm regex fun, but this can't be done only with regex.

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum