Email Address format???Edit: form validation...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Email Address format???Edit: form validation...

Post by JellyFish »

How do I check if a input field was given the value of an email address format (i.e. emailaddr@webpage.com), by the user? I'm doing a sign-up page and need to make sure the type an email address in one of many fields and that it be correctly formatted.

I'm sure there's a way of doing this that is commonly used. Thanks in advance. :D

Edit: I forgot to mention the other input fields in the form that need to be validated, which are:

Phone number: which would look something like "740-899-3777"
Zip: which need to be numbers.

I'm sorry I forgot to mention this. But thanks for the posts and I'll be checking back occasionally.

Thanks.
Last edited by JellyFish on Fri Jun 16, 2006 9:14 pm, edited 2 times in total.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Normally you would use a regular expression along with preg_match().

There are literally so many regular expression solutions for this, you just have to do some reading and pick one that looks reasonable to you. I use a regex on the client side via JavaScript prior to submission, then make a second check within the PHP script.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm... be careful though, a lot of filters aren't totally correct. Look for mentions of adherance to the RFC.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Email Address format???

Post by Roja »

EasyGoing wrote:How do I check if a input field was given the value of an email address format (i.e. emailaddr@webpage.com), by the user? I'm doing a sign-up page and need to make sure the type an email address in one of many fields and that it be correctly formatted.

I'm sure there's a way of doing this that is commonly used. Thanks in advance. :D
No problem: RFC Compliant email validation function.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

There's the link. I was looking for it.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Ambush Commander wrote:There's the link. I was looking for it.
The slightly bad news is that the link will soon change. We're moving away from Gna. Sourceforge got their act together, and our SVN tree there is finally up to date. (YAY!).
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

i just used the function in the SVN link you gave me. Hopefully that'll be okay. I don't think it'll be a problem. Thanks guys. :D
Zythan
Forum Newbie
Posts: 16
Joined: Wed Apr 12, 2006 3:13 am

Re: Email Address format???

Post by Zythan »

Hello,
You will have to forgive but I have put

Code: Select all

if (validateEmailFormat($_POST[email])) {
   echo "A valid email was found.";
} else {
   echo "An invalid email was found.";
}
in my code, is this how the call to the function should be handled ?

TIA

Zythan
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

That is how I did it. :)
Zythan
Forum Newbie
Posts: 16
Joined: Wed Apr 12, 2006 3:13 am

Re: Email Address format???

Post by Zythan »

Hello,

Would any of these be considered as valid email addresses ?

jean.dupont@free.236.com.1
jean.dupont@free.236.com1
fred.bloggs@orange_co.uk
fred_bloggs@orange_co.32

TIA

Zythan
Zythan
Forum Newbie
Posts: 16
Joined: Wed Apr 12, 2006 3:13 am

Post by Zythan »

Hello,

I have continued to test this function, but it would appear not to be working correctly, has anyone else experienced that the function will not identify correctly certain email addresses.

TIA

Zythan
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Email Address format???

Post by Roja »

Zythan wrote:Hello,

Would any of these be considered as valid email addresses ?
It may seem odd, but yes. They are valid email addresses - according to the RFC.

Here is why on each:

jean.dupont@free.236.com.1 - You could have a local intranet named "1". In such a scenario, you can define subdomains like com.1, 236.com, and yes, even free.236.com.1 jean.dupont is a completely valid first portion of the email.

jean.dupont@free.236.com1 - Again, you could have a local intranet named "com1".

fred.bloggs@orange_co.uk - Nothing wrong here. You can have a local intranet named .uk, and you can indeed make it have a subdomain orange_co.

fred_bloggs@orange_co.32 - Still not a problem. The intranet name is "32", and the subdomain is orange_co. Underscores in the opening portion of the name are legal as well.

You seem to be hoping that the RFC prevents unroutable emails, which it is not designed to do. If you have that as a goal, then you will want to do a second step of validation - namely, doing a dns lookup on the portion of the email after the @ sign.

The RFC isn't intended to ensure valid dns addressing. Remember, the email address format came about *before* the internet existed, when local nets were the norm, not working dns. In the years since, the value in allowing LAN emailing addresses to work continued to be valuable.

But even with a validation of the RFC format, AND a check of the dns, you still have no guarantee of a routable email. You can then also go further by checking to see:

- If the dns has a MX record
- If the domain has a mailserver responding at all - and at the MX record (which may be a *different* domain)
- If the mailserver, responding at the IP given by the MX record is willing to accept an email at all (most lie to avoid spam harvesting)
- If it does all of the above, you can send, and it could STILL be refused.

The point is, email is tricky. There are steps along the way, and just because *one* step doesn't do all of them, that doesn't make it invalid. The function is accurate to the RFC. You are expecting too much from the RFC.
Zythan
Forum Newbie
Posts: 16
Joined: Wed Apr 12, 2006 3:13 am

Post by Zythan »

Hello,

Thanks for your explaination, this clears up a few points that may not be evident for a general user.

My goal as you put it, is to know in what situation the function may or may not work as someone might expect, and as you point out there may be a requirement for a second step of validation.
Roja wrote:You are expecting too much from the RFC.
No !

Most general users do not even look at the RFC, therefore they do not even know what it contains, they just use a function they found somewhere and because it works as they expect it to they go no further. A standard case of RTFM before and after.

Please do not get me wrong, I think that the function is very good, but a little basic documentation would be very helpfull.

Thanks again for your reply.

Zythan
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Zythan wrote:I think that the function is very good, but a little basic documentation would be very helpfull.
Fair enough.

It returns a simple true/false boolean based on checking an email against the RFC.

What else should the documentation contain?
Zythan
Forum Newbie
Posts: 16
Joined: Wed Apr 12, 2006 3:13 am

Post by Zythan »

Hello,

The documentation might simply contain the statement "It returns a simple true/false boolean based on checking an email against the RFC. " with a simple example, like

Code: Select all

if (validateEmailFormat(email_text) {
   echo "A valid email was found.";
} else {
   echo "An invalid email was found.";
}
together with your last explaination.

Zythan
Post Reply