2

I'm looking for the most suitable way of validating email addresses for a Norwegian web site I'm working on. I first thought of using PHP's filtering function filter_var() with the FILTER_VALIDATE_EMAIL filter. Just what I need, right? Well, the thing is that here in Norway you may occasionally bump into e-mail addresses with letters like æ, ø and å.

So, since the FILTER_VALIDATE_EMAIL filter doesn't support characters like æ, ø and å, I'm starting to think that I really can't use the filter_var() function at all - at least not on non-English web sites, right?

I want to hear what you guys think of this. Does this mean I should write most of the validation by hand? Is preg_match() a better alternative compared to filter_var() when it comes to validation in PHP?

Example:

//$email = "ørnulf.åsen@gmail.com";
$email = "ørnulf.åsen@gmæil.com";

// How I would do it with preg_match()
$ok_characters = '[a-zæøå0-9!#$%&\'*+-\/=?^_`{|}~]';
$pattern = '/^' . $ok_characters . '+(\.' . $ok_characters . '+)*@' . $ok_characters . '+(\.' . $ok_characters . '+)*(\.[a-z]{2,4})$/i'; // [a-z]{2,4} should be replaced with the most known top-level domain names
if (preg_match($pattern, $email)) {
    echo 'this regexp thinks ' . $email . " is valid.";
} else {
    echo 'this regexp thinks ' . $email . " is invalid.";
}

// How I would do it with filter_var()
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo 'filter_var thinks ' . $email . " is valid.";
} else {
    echo 'filter_var thinks ' . $email . " is invalid.";
}

Result:

preg_match() validates both email addresses as valid. But the filter_var() function thinks both are invalid.

Any suggestions?

olaussem
  • 21
  • 3

1 Answers1

0

non-english characters should not be allowed in the local part of an email-address, i do not think that any email provider would accept the registration of an email-adress you mentioned, because many email servers won´t accept messages from adresses like this. You can take a look the specifications of the local part of email-adresses: Wikipedia: Email Address Local Part Since that FILTER_VALIDATE_EMAIL should always fit your needs.

herrhansen
  • 2,413
  • 1
  • 14
  • 15
  • This regulation has changed. Now you can use all special characters in all the world's languages. See http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address – olaussem Jun 05 '14 at 16:06