chandan Site Admin
Joined: 29 Feb 2004 Posts: 317 Location: India
|
Posted: Sun Feb 29, 2004 10:15 pm Post subject: a good PHP snippet. email validation. |
|
|
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
 |
|