Page 1 of 1

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

Posted: Fri Jun 16, 2006 7:56 pm
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.

Posted: Fri Jun 16, 2006 8:09 pm
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.

Posted: Fri Jun 16, 2006 8:19 pm
by Ambush Commander
Hmm... be careful though, a lot of filters aren't totally correct. Look for mentions of adherance to the RFC.

Re: Email Address format???

Posted: Fri Jun 16, 2006 8:26 pm
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.

Posted: Fri Jun 16, 2006 8:28 pm
by Ambush Commander
There's the link. I was looking for it.

Posted: Fri Jun 16, 2006 9:00 pm
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!).

Posted: Fri Jun 16, 2006 9:06 pm
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

Re: Email Address format???

Posted: Sat Jun 17, 2006 7:13 pm
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

Posted: Sun Jun 18, 2006 4:28 pm
by JellyFish
That is how I did it. :)

Re: Email Address format???

Posted: Sun Jun 18, 2006 5:37 pm
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

Posted: Mon Jun 19, 2006 6:50 pm
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

Re: Email Address format???

Posted: Mon Jun 19, 2006 7:18 pm
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.

Posted: Tue Jun 20, 2006 2:27 am
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

Posted: Tue Jun 20, 2006 7:34 am
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?

Posted: Wed Jun 21, 2006 5:37 pm
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