0

I am trying to make a feedback form for my website, and I want to make the form check if the email is valid (If it has @ and .) Here is the code: http://pastebin.com/9kTEh631

Thank you.

3 Answers3

3

A way to The best way to validate an email address is using PHP's built in validation:

http://php.net/manual/en/filter.examples.validation.php

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
}
?>

It's simple, effective and reliable.

The comment from symcbean is worth adding to this answer as it isn't missed:

The best way to validate an email is to send an email to the address with a link containing a one time pssword. This is just a quick regex check. It does not the domain exists. It does not check the domain has an MX record. It does not check the MTA is accepting mail. It does not check that the mailbox is accepted. It does not check the email is deliverable....

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • Beat you to it by 7 seconds for the exact same answer! – Niels Keurentjes Dec 13 '14 at 00:02
  • The best way to validate an email is to send an email to the address with a link containing a one time pssword. This is just a quick regex check. It does not the domain exists. It does not check the domain has an MX record. It does not check the MTA is accepting mail. It does not check that the mailbox is accepted. It does not check the email is deliverable.... – symcbean Dec 13 '14 at 00:04
  • @symcbean that was never the question, he never asked for "whether an email adress actually exists, is configured correctly and able to receive email", nor anything with "one time passwords". He explicitly asked for how to check for `@` and `.`, and got better answers than that. – Niels Keurentjes Dec 13 '14 at 00:06
  • @symcbean Actually, that's a great point you make. I added it tot he answer and said it was from you. I hope you don't mind. I just don't want it to be overlooked in comments. – Fluffeh Dec 13 '14 at 00:07
0
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
}

Straight from the PHP manual, first hit on Google when searching for php validate email.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
0

you can use filter_input() method , see documentation

example

if(!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
  {
  echo "E-Mail is not valid";
  }
else
  {
  echo "E-Mail is valid";
  }
A.B
  • 20,110
  • 3
  • 37
  • 71