473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ereg statements

Is this a good ereg statement?

if
(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$",$email_1
or $email_2))
{
unset($_GET['do']);
$message_new = "<font color='#cc0000'>$email_1 or $email_2 is not a
valid email address.
Please try again.</font>";
include("login_form.php");
exit();

I am just trying to check if the e-mail fields _1 & _2 on the form contain
valid characters, without getting to complicated. The form I am using it in
keeps returning that I am not entering a valid e-mail address, such
"ch**********@ntlworld.com", or "ch***@myweb.co.uk",or "ch***@ntlworld.com"
not sure why this is the case?

Regards,
C.B.
Sep 30 '06 #1
6 1620
Cerebral Believer wrote:
Is this a good ereg statement?
The regular expression on itself is fine, but the error is the '$email_1 or
$email_2' condition, which evaluates to '1' or '' before it's compared. You
need to apply the regular expression for each email address seperately.

Also, it's recommended to use the PCRE library instead of the POSIX-based
ereg* functions. See http://www.php.net/pcre for more information.
JW
Sep 30 '06 #2
In article <kd*******************@newsfe6-gui.ntli.net>, Cerebral
Believer <no**********@hadenoughalready.comwrote:
Is this a good ereg statement?

if

(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,})
{1}$",$email_1
or $email_2))
{
unset($_GET['do']);
$message_new = "<font color='#cc0000'>$email_1 or $email_2 is not a
valid email address.
Please try again.</font>";
include("login_form.php");
exit();

I am just trying to check if the e-mail fields _1 & _2 on the form contain
valid characters, without getting to complicated. The form I am using it in
keeps returning that I am not entering a valid e-mail address, such
"ch**********@ntlworld.com", or "ch***@myweb.co.uk",or "ch***@ntlworld.com"
not sure why this is the case?

Regards,
C.B.


Try this:

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

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Oct 13 '06 #3
Koncept:
[Cerebral Believer:]
Is this a good ereg statement?
No. The pattern tries to weed out malformed e-mail addresses but fails
because it is too restrictive. A regular expression to match all
well-formed e-mail addresses would be complex, since in addition to the
productions you would need to include from RFC2822, the pattern would
involve recursion of a subpattern if it was to allow comments.
Try this:
A prime bogosity indicator on Usenet.

--
Jock

Oct 13 '06 #4
John Dunlop wrote:
Koncept:

>>[Cerebral Believer:]

>>>Is this a good ereg statement?


No. The pattern tries to weed out malformed e-mail addresses but fails
because it is too restrictive. A regular expression to match all
well-formed e-mail addresses would be complex, since in addition to the
productions you would need to include from RFC2822, the pattern would
involve recursion of a subpattern if it was to allow comments.

>>Try this:


A prime bogosity indicator on Usenet.
I did see a regex which would handle all possible addresses. Don't
remember where I saw it off hand, but it was around 6500 chars long.

Obviously it's not easy to validate email addresses!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 13 '06 #5
Jerry Stuckle:
I did see a regex which would handle all possible addresses. Don't
remember where I saw it off hand, but it was around 6500 chars long.
To give a rough idea to anyone who thinks it's easy, Jerry, point your
browser here:

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

That regular expression purportedly validates the well-formedness of
RFC822 e-mail addresses excluding comments. In other words even that
monster couldn't validate e-mail addresses itself. Since RFC2822
obsoleted RFC822, the pattern would need to be even larger, because
RFC2822 allows obsolete productions from RFC822 as well as its own.
Obviously it's not easy to validate email addresses!
Not half.

(Slightly pedantic, but validating e-mail addresses and validating
e-mail address syntax are two different things. Validating e-mail
addresses entails dicovering if the e-mail address is in use;
validating e-mail address syntax entails checking that the syntax
matches that allowed by RFC2822. Go on, witty retort: 'you're not
being pedantic; you're being pernickety'.)

Enjoy the weekend!

--
Jock

Oct 13 '06 #6
John Dunlop wrote:
Jerry Stuckle:

>>I did see a regex which would handle all possible addresses. Don't
remember where I saw it off hand, but it was around 6500 chars long.


To give a rough idea to anyone who thinks it's easy, Jerry, point your
browser here:

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

That regular expression purportedly validates the well-formedness of
RFC822 e-mail addresses excluding comments. In other words even that
monster couldn't validate e-mail addresses itself. Since RFC2822
obsoleted RFC822, the pattern would need to be even larger, because
RFC2822 allows obsolete productions from RFC822 as well as its own.

>>Obviously it's not easy to validate email addresses!


Not half.

(Slightly pedantic, but validating e-mail addresses and validating
e-mail address syntax are two different things. Validating e-mail
addresses entails dicovering if the e-mail address is in use;
validating e-mail address syntax entails checking that the syntax
matches that allowed by RFC2822. Go on, witty retort: 'you're not
being pedantic; you're being pernickety'.)

Enjoy the weekend!
You've got it! :-)

And BTW - you right about the difference. And you can't validate an
email address. Nowadays too many ISP's bit-bucket mail to invalid
addresses rather than return a message. Too many spammers were using
the lack of a response to indicate they hit a valid email address.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan Gangefors | last post by:
I'm trying to figure out what I'm doing wrong when using ereg(). This is my regexp: ereg("^]$", "]"); and that does'n work, but this does: ereg("^$", "[");
9
by: Fiore Alessandro | last post by:
Hi all I've installed Apache 2.0.48 and PHP 4.3.4 on a workstation running Linux, RedHat 9.0. Let's consider the following piece of code: $patt = "{3}/{6}"; if...
3
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an...
2
by: pomho | last post by:
Hi all, Even having read the PHP Manual, I can't understand the difference between the ereg function and the preg_match... Could anyone help ? thx in advance, --
3
by: Rafal Zak | last post by:
I have a little problem with ereg (eregi) in PHP - in some cases it behaves differently than I expect and I don`t know if my expectations are strange or there is any "syntax subtlety" I have...
3
by: Dynamo | last post by:
Hi, Thanks to all who replied to original posting.As a direct result of now gaining a better understanding of regular expressions, instead of trying to use other peoples scripts I am now trying to...
3
by: news | last post by:
I'm trying to make sure a form has only one or two digits in either of two fields. I looked at php.net and http://www.regular-expressions.info/reference.html and this is what I put together, but...
3
by: rachellara1979 | last post by:
Hi, new to PHP and to this group, so very sorry if this is simple or has been covered before. Basically want to check that a user and password (entered in a previous page) only has characters but...
18
by: yawnmoth | last post by:
Say I have the following script: <? $string = 'test'; if (eregi("^+$",$string)) { echo 'matches!'; } else {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.