Page 1 of 1
email address type
Posted: Thu Sep 29, 2011 6:13 pm
by nite4000
I need to know how I would check to see if a type of email address is been entered proberly on submit but not sure how i would write teh check code
like i need it to only allow gmail and hotmail email addresses and have it error out if they entered anything other then that.
Thanks
Re: email address type
Posted: Thu Sep 29, 2011 10:05 pm
by twinedev
Well would depend, what are you considering "only gmail and hotmail"? should it just have just @hotmail.com or @gmail.com (not sure what else they may use for other countries) a lot of hotmail accounts also use other domains (@msn.com @sbcglobal.com for their subscribers)
But if you are just going off of the two main domains:
Code: Select all
if (!preg_match('/^[a-z0-9._%+-]+@(gmail|hotmail)\.com$/i',$_POST['txtEmail'],$regs)) {
echo "This is not a valid e-mail address.";
}
else {
echo 'Nice to see a ' . $regs[1] . ' user! ';
}
Ok, so this did more than you asked, it not only checked, but then also put the domain in the variable $regs[1] (either "google" or "hotmail" )
So taking that, and expanding further you can do this:
Code: Select all
$aryHosts = array('gmail.com','hotmail.com','yahoo.com','yahoo.co.uk');
if (!preg_match('/^[a-z0-9._%+-]+@([a-z0-9.-]{4,})$/i',$_POST['txtEmail'],$regs)) {
echo "This is not a valid e-mail address."; // General format/valid characters
}
elseif (!in_array($regs[1],$aryHosts)) {
echo "Sorry, that is not a valid host for e-mail"; // Didn't match proper host
}
This method is more easily expandable. All you have to do is add/remove items from the array in the first line, but if you are for sure just only doing gmail.com/hotmail.com, then the first method is better IMO.
-Greg
Re: email address type
Posted: Fri Sep 30, 2011 4:39 am
by nite4000
Thanks for the reply I will be trying it soon and hope it will work for what i need.
Thanks
Don
Re: email address type
Posted: Fri Sep 30, 2011 1:27 pm
by nite4000
I did try the code you supplied however i need to add it into my code that checks for email validation
here is my current code i have before yours
Code: Select all
if (strlen($_POST['email']) > 0)
{
if (valid_email($_POST['email']) == TRUE)
{
$email = $_POST['email'];
}
else
{
$error = TRUE;
$msg_email .= '<li>Invalid email!</li>';
}
}
else
{
$error = TRUE;
$msg_email .= '<li>Please enter your e-mail address!</li>';
}
I need to add your code this..
Code: Select all
$aryHosts = array('gmail.com','hotmail.com','yahoo.com','yahoo.co.uk');
if (!preg_match('/^[a-z0-9._%+-]+@([a-z0-9.-]{4,})$/i',$_POST['txtEmail'],$regs)) {
echo "This is not a valid e-mail address."; // General format/valid characters
}
elseif (!in_array($regs[1],$aryHosts)) {
echo "Sorry, that is not a valid host for e-mail"; // Didn't match proper host
}
so it will all work together.