Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email validation... need some help with this! 2

Status
Not open for further replies.

SPYDERIX

Technical User
Jan 11, 2002
1,899
CA
Hi,

I have checked out the FAQ area and found a huge script for email validation but unfortunately it's a bit complex for me right now and I only need something simple.

All I want to have is a script that will check to make sure that there are no spaces or weird characters that aren't allowed in email syntax, only one @-sign, a top level domain and make sure that the format follows this:

[text and/or #] one @-sign [text and/or #] .top level domain

I came across a script and have been playing around with it but it doesn't quite work to the extent that I need it to. Can someone please help me out?

[-----SCRIPT-----]
<?php
$evalid=2;
if(ereg(&quot;([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)&quot;, $email))
{
$evalid=1;
}
else
{
$evalid=2;
}

##### Below part will appear much later in my script #####

if ($evalid=='1')
{
echo &quot;VALID EMAIL\n&quot;;
}
else
{
echo &quot;INVALID EMAIL\n&quot;;
}
?>
[-----SCRIPT-----]

The script recognizes the format (sort of) but it allows spaces and 2 @ signs and says that 1@2. is valid, but clearly it is not.

Thanks for any help!
relax.gif

 
SPYDERIX:

That FAQ you're talking about is mine. Here's the basics.

Don't try to write a single regular expression that will check that an email address is well-formed. It can be done, but you'll make yourself crazy.

Split the potential email address at the &quot;@&quot;-sign. Make sure you have exactly two pieces.

Then check each piece separately for well-formedness. Want the best answers? Ask the best questions: TANSTAAFL!
 
Hi sleipnir214,

I understood that that was what your FAQ did. But that's about all I understand. I am very new to PHP and don't know how to go about doing what you just mentioned. Basically with your FAQ I would like it to check all those TLD's without needing to check the checkbox and then simply have a variable that stores a number, 1 being valid and 2 being invalid, then later in my form validation script I will need to do a check to see if the variable is a 1 or a 2 and then go from there.

Can you help me out a bit more! Sorry if I seem a bit ignorant with regards to this, still learning.

Thanks!
relax.gif

 
I only provided that HTML code as an example driver for the script. There is nothing that states you have to use it.

If you want to have good_email() check whether an address is well-formed, invoke it as: good_email(<some potential email address>);

If you want good_email() to check whether it is both well-formed and that the domain exists within a possible TLD, invoke the function as: good_email (<some potential email address>, TRUE);

good_email() will return TRUE if it finds that the email is good, and FALSE if it does not. Want the best answers? Ask the best questions: TANSTAAFL!
 
Have you come up with a solution? If not, this codes might help. I've seen it in the forum but I can't remember where. So here it is:

if (eregi(&quot;^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z]){2,3}$&quot;, $email))
echo &quot;Valid Email Address&quot;;
else
echo &quot;Invalid Email Address;

 
Hi roswell,

I have found a solution.

I have seen that exact code you posted, however there is a parse error it that code and it doesn't actually work.

There is also a very similar one found on zend.com but it too doesn't work. Strange that a site like zend posts a tutorial on how to do this and screws up the code.

Anyway, this is what I used and it works really well:


<?php
$evalid=2;

if (eregi(&quot;^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$&quot;,$email))
{
$evalid=1;
}

else
{
$evalid=2;
}

?>


Thanks for your help!
relax.gif

 
Here's a slightly different expression that has done me well...

<?php
$evalid=2;
if (eregi(&quot;^([[:alnum:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$&quot;,$email))
{
$evalid=1; //OKAY
} else {
$evalid=2; //NOT OKAY
}
?>
 
DOH! #@$%^! Smileys!

REPOST:

Here's a slightly different expression that has done me well...

<?php
$evalid=2;
if (eregi(&quot;^([[:alnum:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$&quot;,$email))
{
$evalid=1; //OKAY
} else {
$evalid=2; //NOT OKAY
}
?>
 
if( !ereg( &quot;^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$&quot;, $mail ) ){
print &quot;Wrong mail address.<BR>\n&quot;;
$valid = false;
} else {
print &quot;Mail is Ok.<BR>\n&quot;;
$valid = true;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top