Web Hosting Talk







View Full Version : [PHP] Slight amendment to email validation script


jonathanbull
07-17-2005, 09:59 PM
Hi,

I'm using a script to check the validity of an email address, here's the main part of it:

if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$email)) {

Using this script, the email name@name.com will be seen as valid.


I'd like to change the script so it doesn't check the extension part, so that name@name would be seen as valid.

Is there any way of doing this? I keep getting various errors when I try - I obviously have much to learn!


Any help would be greatly appreciated!

tiamak
07-17-2005, 10:36 PM
i think you should remove \.[a-z]{2,4}

w3needs
07-17-2005, 10:48 PM
Eliminate the part you don't need:

^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*

name@name.com and name@name both come across as valid. Or, if you want to fully eliminate the dots/periods,

^[0-9a-zA-Z]([-_]?[0-9a-z])*@[0-9a-zA-Z]([-]?[0-9a-z])*$

The dollar sign ends the string checked, so name@name.com would not match, but name@name would. Take out the dollar sign, and you will find that both, name@name.com and name@name match.


Also, use the following resources:

REGEXlib (http://regexlib.com) - allows you to test with an online pattern tester, and has many patterns to use in applications.

regular-expression.info (http://regular-expression.info/) - great site for info and downloads

REGEX buddy (http://www.regexbuddy.com/) - downloadable regular expression checker.

Hope it helps.

jonathanbull
07-17-2005, 10:55 PM
Thank you tiamak, that's exactly what I was looking for.

And a double thank you to w3needs. Those links are extremely useful, and I love knowing how to do something so I won't need to ask in the future.


Thanks again!

jonathanbull
07-17-2005, 11:00 PM
Just another quickie!

As suggested, I'm now using:

if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*$",$email)) {

Using this, name@name.com and name@name are accepted as valid email addresses.

Unfortunately, name@name. is seen as invalid. Is there any way of accepting this as well?


Thanks once again, hopefully this'll be the last...!

tiamak
07-17-2005, 11:26 PM
well
simpliest way would be
^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[\.0-9a-z])*$

but then:
name@domain.com is VALID
name@domain is VALID
name@domain. is VALID
and im not sure if you want it

tiamak
07-17-2005, 11:39 PM
for strict validation only for
anyname@name. anyname@name anyname@n-a-m-e anyname@n4m-3. etc...
(name can contain a-z 0-9, - is allowed in string, can have optional . at the end)
"^[0-9a-z]([-_.]?[0-9a-z])*@([-]?[0-9a-z])*\.?$"

jonathanbull
07-17-2005, 11:48 PM
Thank you tiamak. The first one was exactly what I was looking for.

I must be getting extremely annoying now, but is it easy to also make it so that:

anyname@.
and
anyname@.ext is valid?


Thanks again, you've been a massive help. This would've taken me weeks to figure out...!

tiamak
07-17-2005, 11:57 PM
"^[0-9a-z]([-_.]?[0-9a-z])*@([-_\.0-9a-z])*$"
anyname@anything that can contain - _ . 0-9 a-z :)

lol my #100 post

jonathanbull
07-18-2005, 12:03 AM
Thanks (again!) tiamark. Almost there!

anyname@ is now valid, which I didn't want. Is there any way to fix this?

tiamak
07-18-2005, 12:26 AM
thats not exactly what you want but you may try
"^[0-9a-z]([-_.]?[0-9a-z])*@([-_\.0-9a-z]{1,70})$"
this will limit string after @ to contain 1 to 70 chars (usually domains should not be longer than 63 chars so limit string to 70 seems to be ok as for me :D)

im too dumb in this area (regexp) :D to get you better solution

beside its 6:25 AM and my brain does not work anymore so i think ill go to bed now :)
cya

Koobi
07-18-2005, 09:05 AM
do you mind making a post with exactly what you expect the expression to match in point form?
i think your expression can be smaller.

if you can make the post, i would be willing to have a look at it :)

michael-lane
07-18-2005, 10:17 AM
how does all that gibble validate it?

jonathanbull
07-18-2005, 10:22 AM
Thanks Koobi,

Here's what I'm aiming for:


- The email has to have at least one character after the @ sign.

- The characters after the @ sign can only be letters, numbers, or a fullstop.


That's about it!

Koobi
07-18-2005, 12:24 PM
ok, i will assume you mean alphabets, numbers as well as hyphens, underscores and periods when you say "characters" (for the characters before the "@")

this should work (untested):

"|[-_\.a-z0-9]+@[\.a-z0-9]+|i"




explanation:
[-_\.a-z0-9]+ => one or more (signified by the +) characters of a hyphen, underscore, period, alphabets, digits
@ => the character "@"
[\.a-z0-9]+ => one or more (signified by the +) characters of a period, alphabets, digits

the i at the end signifies that the match should be case insensitive. it's called a "modifier"


if you want to, you can remove the hyphen and underscore in the first part of the expression...just remember to always add the hyphen as the first thing if it's within the square brackets because hyphens have a special meaning within a set of square brackets unless it appears as the first character.


hope it helps :)
let me know if it doesn't work as expected or you need a modification.