473,562 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with email address validation using eregi()

Hi

The following script was taken from John Coggeshall's (PHP consultant) in his
article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php

// Get the email address to validate
$email = $_POST['email']
// Use John Coggeshalls script to validate the email address
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {
echo "The e-mail was not valid";
}
else {
echo "The e-mail was valid";
}

In the first instance the script does not work if I copy and paste it into my
php file. I get a parse error message.
In the second instance, there are many postings relating to the scripts
effectiveness in that it will still allow email addresses that are invalid.
So I have 3 questions.
1) Why do I get the parse error message?
2) Is there a definitive email validation script?
3) Where can I find regular expressions or other form validation scripts to
validate things such as telephone numbers, postcodes, names that don't contain
numeric characters etc etc

Kind regards
Dynamo

Jul 17 '05 #1
25 6453
Dynamo wrote:
The following script was taken from John Coggeshall's (PHP consultant) in his
article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php

// Get the email address to validate
$email = $_POST['email']
Missing semicolon at the end.
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email)
Too strict and too liberal at the same time.

[ ... ]
So I have 3 questions.
1) Why do I get the parse error message?
Ask PHP to tell you, not us.
2) Is there a definitive email validation script?
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html is
often put forward here, but it requires Perl to fully
validate an address because the pattern itself does not
handle comments.
3) Where can I find regular expressions or other form validation scripts to
validate things such as telephone numbers,
DIY:

http://www.geocities.com/dtmcbride/r...e/tel-fmt.html
postcodes,
UK:

`([A-Z]{1,2})(\d[A-Z\d]?)\040(\d)([ABD-HJLNP-UW-Z]{2})`
names that don't contain numeric characters
Don't understand.
etc etc


Cannot compute.

--
Jock
Jul 17 '05 #2
In article <MP************ ************@Ne ws.Individual.N ET>, John Dunlop says...
// Get the email address to validate
$email = $_POST['email']


Missing semicolon at the end.
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email)


Too strict and too liberal at the same time.

[ ... ]
So I have 3 questions.
1) Why do I get the parse error message?


Ask PHP to tell you, not us.


Granted the semi-colon is missing in my post but it is present in my actual
script.

PHP simply sais Parse error. Unexpected '}' but I cannot see where.

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {
echo "The e-mail was not valid";
}
else {
echo "The e-mail was valid";
}

This following script is taken from a posting by an 11 year old to validate
passwords.

if (!ereg('^[[:alnum:]]+$', $pass)) {
$err14 = "Password contains illegal characters</br>";
$errors = 1;
}

AND IT WORKS! Now if an 11 year old can do that and yet a php consultant posts a
script that doesn't work then where does that leave a 50 year old newbie like
me! DAZED and CONFUSED.

Regards
Dynamo

Jul 17 '05 #3
Carved in mystic runes upon the very living rock, the last words of
Dynamo of comp.lang.php make plain:
The following script was taken from John Coggeshall's (PHP consultant)
in his article on Zends site at
http://www.zend.com/zend/spotlight/ev12apr.php

// Get the email address to validate
$email = $_POST['email']
// Use John Coggeshalls script to validate the email address
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) { 1) Why do I get the parse error message?
You're missing a closing parenthesis at the end of your condition. But
this regex is not valid anyway. I don't know about it passing invalid
address, but it will not pass many valid ones. The fact is, virtually
anything is allowed to the left of the @ sign, and all regexes I've seen
for email validation fail to allow for this. Further, something like
"fp***********@ ufidos.yy.zzz" would pass, but is obviously not a valid
address. The way to validate an email address is to break it down into
its parts and analyze the parts, for example:
2) Is there a definitive email validation script?
There's a pretty good email validation function in HoloLib:

ftp://ftp.holotech.net/hololib.zip
3) Where can I find regular expressions or other form validation
scripts to validate things such as telephone numbers, postcodes, names
that don't contain numeric characters etc etc


http://www.phorm.com/

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #4
Dynamo <Dy***********@ newsguy.com> wrote:
1) Why do I get the parse error message?
Ask PHP to tell you, not us.


Granted the semi-colon is missing in my post but it is present in my actual
script.


Then post the real code!
PHP simply sais Parse error. Unexpected '}' but I cannot see where.
Is this the _real_ error? Are you sure it isn't: Unexpected '{' ?
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {
There is a ')' missing here ^
This following script is taken from a posting by an 11 year old to validate
passwords.

if (!ereg('^[[:alnum:]]+$', $pass)) {
$err14 = "Password contains illegal characters</br>";
$errors = 1;
} AND IT WORKS!
No it doesn't... all it does is force weak passwords
Now if an 11 year old can do that and yet a php consultant posts a
script that doesn't work then where does that leave a 50 year old
newbie like me! DAZED and CONFUSED.


What are you trying to accomplish? IMHO email "validation " in PHP is a
total waste of time and resources. The regexp you found "validates"
the obvious invalid '-@-.xx' as a valid emailaddress, and the very possibly
valid 'f****@last.nam e' as invalid.

If you want to make sure the email is valid, the only way is to actually
send a challenge to the emailaddress and wait for the response.

Jul 17 '05 #5
In article <Xn************ **************@ 216.196.97.132> , Alan Little says...
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {

1) Why do I get the parse error message?


You're missing a closing parenthesis at the end of your condition.


In my original post the closing parenthisis was there in the else part of the
statement or am I going mad?

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {
echo "The e-mail was not valid";
}
else {
echo "The e-mail was valid";
} // closing parenthisis is here

But thanks for the other info.
Regards
Dynamo
Jul 17 '05 #6
Carved in mystic runes upon the very living rock, the last words of
Dynamo of comp.lang.php make plain:
In article <Xn************ **************@ 216.196.97.132> , Alan Little
says...
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-
]+)*(\.[a-z]{2,3})$", $email) {
1) Why do I get the parse error message?


You're missing a closing parenthesis at the end of your condition.

In my original post the closing parenthisis was there in the else part
of the statement or am I going mad?
You're thinking of the curly braces for the code block. I'm talking about
the parenthesis around the condition. You have:
if(!eregi("[the pattern]", $email) {

^
parenthesis missing
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #7
Carved in mystic runes upon the very living rock, the last words of
Daniel Tryba of comp.lang.php make plain:
What are you trying to accomplish? IMHO email "validation " in PHP is a
total waste of time and resources.

If you want to make sure the email is valid, the only way is to
actually send a challenge to the emailaddress and wait for the
response.


Ultimately, yes, that's the only way to know if an email address is valid:
send something and see if it bounces. But you can do some pre-checking that
is worthwhile for catching things like typos and so forth.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #8
I can answer one of your questions...

This line:
$email = $_POST['email']

needs to be this:
$email = $_POST['email'];

I would also take a look at this site:

http://www.webreference.com/programming/php/regexps/
You may just want to make your own regular expressions.

Jul 17 '05 #9
I can answer one of your questions...

This line:
$email = $_POST['email']

needs to be this:
$email = $_POST['email'];

I would also take a look at this site:

http://www.webreference.com/programming/php/regexps/
You may just want to make your own regular expressions.

Jul 17 '05 #10

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

Similar topics

3
12679
by: JDJones | last post by:
I have a script that uses the following eregi to check for a valid email address: if (!eregi("^+@(+\.)+{2,4}$", $email)) { print_error("your <b>email address</b> is invalid"); } While this works fine on most email addresses, it will not allow anyone who has more than one dot in their domain name (like me@myisp.co.uk) to submit. How can...
7
1721
by: MJ | last post by:
I'm having problems coming up with a regular expression that works for what I want. I need to extract a few numbers from from html. Here's a snippet: <tr><td align="right">Consolidated Metro Area (CMSA)</td><td align="left"><strong> SEATTLE-TACOMA-BREMERTON, WA ( 7602 )</td></tr> <tr><td align="right">Primary Metro Area (PMSA)</td><td...
0
2791
by: mcp6453 | last post by:
I am trying to use Jack's FormMail script (http://www.dtheatre.com/scripts/formmail). Since I'm brand new at PHP and not very good at HTML, I have an easy question, which I will narrow down. When the email arrives, it has this information: v_firstname: asdf v_lastname: asdf b_email: asdf@bellsouth.net v_phone: asdf v_cellphone: asdf
7
3583
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title> </head> <style type="text/css">
0
1478
by: Shaggyh | last post by:
hi im needing help with a program im writing to do subnetting i was on before about it and i got some help. the code below wont work for me and i cant think of why not. i was wondering if anyone out there can help, even come up with a better way of doing this.. i have 2 files.. here is my first file.. // a library assembly i.e. dll
35
3333
by: Mika M | last post by:
Simple question: Does Framework (1.1) contain any routine to check entered email-address is valid ? It's quite easy to make own code for that purpose, but why to do if Framework (1.1) contain this kind of routine. -- Thanks in advance! Mika
4
1860
by: ollie.mitch | last post by:
Hi, I need two ereg expressions in my PHP code. One of them needs to check that a string only contains letters, and the other needs to check that the string only contains letters and commas (only one comma at each time). I thought that the code for only containing letters would be: eregi("^()", $keywords);
5
1822
by: mantrid | last post by:
Up to the other day I have not bothered protecting my php script on my feedback form against email injection. Howerver, i have had a spammer using it to insert email addresses as cc: bc: into my email field. First I was puzzled why he was doing it as the message being sent was just jibberish. I have recently used a function to protect these...
3
1759
by: jerry101 | last post by:
hi, i've got a form in which the user can fill out consisting of 2 text boxes to enter information in, a drop down box of choices and of course a submit button. when they fill it out, it sends the email of information to me fine. everything comes through apart from the option they've chosen from the drop down box. the code i have is the...
1
7625
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6219
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5476
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3621
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2073
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1187
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
901
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.