Free Web Hosting PHP/mySQL
GlobalWeb.com.ru Forum Index GlobalWeb.com.ru
Free Web Hosting :: Commercial Web Hosting :: PHP/mySQL :: CGI/Perl
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

a good PHP snippet. email validation.

 
Post new topic   Reply to topic    GlobalWeb.com.ru Forum Index -> PHP
Author Message
chandan
Site Admin


Joined: 29 Feb 2004
Posts: 317
Location: India

PostPosted: Sun Feb 29, 2004 10:15 pm    Post subject: a good PHP snippet. email validation. Reply with quote

while designing your sites u may come across pages that are secured only for the members and when they sign up, u need to verify their email address if thats correct or not... well here is how u can do it.....

so... lets look at the format of any email address first....

it begins with a string, and the first character of the string cannot be an @ or a space character. this character is repeated any number of times. this is then followed by an @ . after the @ is a character that cannot be an @ or a blank space. this is then followed by a character repeated any number of times. then comes a period folowed by a character that is not a @ or a space which can be repeated any number of times .after this, there is the end of the string.


now that we know the format, we need to write a regular expression. a regular expression matches a pattern in a string. here is the one we will use. ^[^@ ]+@\.[^@ \.]+$. that alone does not check the email for correctness, we need to use ereg(), and place it into a user-defined function like this:


Code:
function EmailCheck($Email) {
    $Checked = ereg("^[^@ ]+@\.[^@ \.]+$", $Email);
    if($Checked) {
        return true;
    } else {
        return false;
    }
}
 




here, in the second line, we use ereg with the arguments of the regular expression, then the string for it to check in. if it works, $Checked will be present. if the email is correct the function will return true or 0, if not it will return false. here is how to check and email.


Code:
if(EmailCheck("Email to Check") == 0) {
    echo "Check was unsuccessful.";
} else {
    echo "Check was successful.";
}
 



the code above checks to see if the function returned 0 or true, and then give the results. here is the final and the complete code u can use....


Code:
<?php

function EmailCheck($Email) {
    $Checked = ereg("^[^@ ]+@\.[^@ \.]+$", $Email);
    if($Checked) {
        return true;
    } else {
        return false;
    }
}

if(EmailCheck("z-team@attasdfa....bic...om") == 0) {
    echo "Check was wrong";
} else {
    echo "Check was successful";
}

?>


thats all.... enjoy.
_________________
get free PHP scripts

Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
popinho



Joined: 12 Mar 2004
Posts: 51
Location: Austria

PostPosted: Fri Mar 12, 2004 12:25 pm    Post subject: Reply with quote

very nice script, thought about scripting such one by myself but you saved a lot of my time. thx Smile
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    GlobalWeb.com.ru Forum Index -> PHP All times are GMT
Page 1 of 1

 


Free Web Hosting, Commercial Web Hosting, PHP/mySQL Copyright © 2009
Free Scripts Directory | Free Software Downloads | Newsgroups Directory | Optel.net | Mirroraty | Cruised.net | Hardware Keylogger