Email addie validation

HI,

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!";
		}


Can anyone please help?

if(!preg_match(‘/[1]{2,30}$/’,$authors_name)){

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.


  1. a-zA-Z\'/.-\s ↩︎

no luck, this wont even allow ‘john’

if(!preg_match(‘/[1]{2,30}$/’,$authors_name)){
$error .= “~ Only letters, spaces, apostrophes and hyphens are allowed for the name field.<br>”;
}


  1. a-zA-Z\'/.-\s ↩︎

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 :slight_smile:

  • Add \s after the - at the end

Is that ok?

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).

Is this right then, for validating a persons name?


if (!eregi("/[a-z'\\s-]*/", $name))
		{
		$errors[] = 'Please enter your name correctly.';
		}

  • Remove the / after the Z
  • Remove the \ before the dot, in a character class the dot doesn’t need escaping
  • Add \s after the - at the end

:slight_smile:

Yes, I have verified this :wink:

if(!preg_match(‘/[1]{2,30}$/’,$authors_name)){
So i add the ‘\s’ where in the code?


  1. a-zA-Z/\'/\.- ↩︎

Why did you switch from $errors = to $error .= half way down this thread? Is that intentional? Might it go wrong somewhere there?

You’ve re-verified that since making all your other changes, the author name you’re typing in is indeed getting forced through your regex now?

Go back to the regex that rejected John Smith and then add back in the \s char.

OK, this seemed to work, however it lets you put in chars like:
John ?/!,

if (!preg_match(‘/[a-zA-Z\’\s-]+/', $authors_name)){
$error .= “Only letters, spaces, apostrophes and hyphens are allowed for the name field.<br>”;
}

is going crazy, this wont validate: John Smith

if(!preg_match('/^[a-zA-Z/\\'/\\.-]{2,30}$/',$authors_name)){ 
    $error .= "Only letters, spaces, apostrophes and hyphens are allowed for the name field.&lt;br&gt;";
    }

(‘/[1]{2,30}$/’

does not have the \s (whitespace) char.

*edit ah now I see you’ve changed it. Does that still not work?

Hm, the ! etc… you should keep your ^ and $ in your regex

*still, I don’t see how those chars can possible match…


  1. a-zA-Z/\'/\.- ↩︎

john ??//,!!$$&& - passed validation

if (!preg_match(‘/[a-zA-Z\’\s-]+/', $authors_name)){
$error .= “Only letters, spaces, apostrophes and hyphens are allowed for the name field.<br>”;
}

:frowning:

This has been enough for me so far:


function checkEmail($email){
        if(!preg_match("/^[^0-9][A-z0-9_-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$/", $email)){
    		return false;
    	}
        return true;
    }

I want to hear some pitfalls on this :wink:

Nevermind, found this great class!!!
http://code.google.com/p/php-email-address-validation/

Thanks to you all for your help :slight_smile:

Scallio,

What regex would I need to validate a name field where I want to allow:
letters, apostrophes, spaces, hyphens?

Also, does the MX check work fine with your list of $mxhosts, assuming the email is valid?

Why not use PHP’s filter extension?


<?php

// ...

if (!filter_var($authors_email, FILTER_VALIDATE_EMAIL)) {
    $error .= 'Invalid email address';
}