I have tried using the following function to validate a submitted email but it wont validate a valid email???
// function used to validate email accounts - also looks up domain name
function checkEmail($email) {
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\\._-] +)+$/" , $email)){
list($username,$domain)=split('@',$email);
if(!getmxrr ($domain,$mxhosts)){
return false;
}
return true;
}
return false;
}
// Check for an email address & make sure there are no errors.
if(!checkEmail($authors_email)) {
$error .= "Invalid email address!";
}
Isn’t this stopping your regex pattern prematurely, even tho the ] still hasn’t come yet? (And, are you trying to allow / characters in names? No)
The \ I get, it’s commenting out (escaping) the ', but the you have the / before the dot. Why? Dots are literal inside char classes. [.] is just a dot.
My $.02? I HATE email validation because people make too many assumptions. Instead of trying to be so strict about the format, just make sure that they tried to enter something. I have a gmail account and I use the + trick (http://lifehacker.com/144397/instant-disposable-gmail-addresses) a ton to keep my inbox clean. Too many times I’ve been rejected by sites and it just frustrates me (and increases the chance of me missing your emails).
I think you are more likely to have someone have a typo in their address than try to beat your system. I don’t understand what everyone is trying to accomplish by creating the perfect regex for email addresses.
[/rant]
Yup, inside a character class you don’t have to escape . and -
Indeed, as a convention some tend to put those at the start of the character class to avoid confusion, but you don’t have to do that
I’ve had it beaten into me to always put those in the beginning or end so nobody can even begin to even think that maybe possibly I’m trying to use it as a range operator (but maybe here nobody can ever possibly think that?? but computers be dumb).
if(!preg_match('/^[a-zA-Z/\\'/\\.-]{2,30}$/',$authors_name)){
$error .= "Only letters, spaces, apostrophes and hyphens are allowed for the name field.<br>";
}