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?