1

1st post here so bear with me lol

just wanted to check my work here i have a form that validates a email address but with the change to php 5.3 now errors

can some one please look over my change and tell me what im missing as it not working

OLD WAY

function valid_email($email)
{
// check an email address is valid
if (ereg('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}

NEW WAY

function valid_email($email)
{
// check an email address is valid
if (preg_match(('^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$', $email))
return true;
else
return false;
}

form sends with no email validation. i no its prob some thing simple but i cant work it out

thanks in advance

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 5
    Nope. The right way is : `if(filter_var($email, FILTER_VALIDATE_EMAIL)) { // valid }` – Amal Murali Oct 28 '13 at 13:34
  • 1
    @AmalMurali You should post that as an answer. – ceejayoz Oct 28 '13 at 13:37
  • Lookout when validating e-mail addresses... Localized domains are here! This would reject an "é" or a russian website. Just to name a VERY FEW. – Salketer Oct 28 '13 at 13:38
  • You say there are errors, but you did not list any errors. Are you considering the form submitting as an error? The form has to submit for the email address to get to PHP to be used in the function you have here. Maybe you would want to consider Ajax to send the email address to PHP and only submit the form if it returns as valid? – jonhopkins Oct 28 '13 at 13:38
  • @ceejayoz: Nope, there's already many similar questions. See Mike's comment above :-) – Amal Murali Oct 28 '13 at 13:42

2 Answers2

1

Do not use regular expressions for validating email addresses or hyperlinks. Also, as far as I know, ereg has been deprecated. Use preg from now on with regular expressions. The code for validating email addresses is:

if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    echo "This email address: $email_address is invalid. Enter a new one please";
}

If you must use a regular expression, use this RFC-822 regex , but that may be a bit too complicated for what you're trying to do.

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
0

First unlike ereg, with preg_match you need to enclose the pattern within a delimiter, which is usually "/" though you can use other characters too if you want. For example,

preg_match('/^pattern$/', $email)

Secondly in your code there is an extra "(" after the function name "preg_match((" which should be "preg_match(" instead.

You might want to consider using the following regular expressions. It prevents domain name starting or ending with a dash such as username@-domain-.com but still allow dash within domain name such as username@my-domain.com

function isValidEmail($email)
{
    return preg_match('/\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z/', $email)
        && preg_match('/^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*/', $email);
}

See validate email address using regular expression in PHP.

Geek
  • 413
  • 6
  • 4