Link to home
Start Free TrialLog in
Avatar of Fajer39
Fajer39

asked on

Html form simple validation

Hi,

when I have a simple single line form only for collecting emails -> What's the best way to validate this email and then send it through PHP to client's email?

I searched many sites but as a newbie in this field it gets confusing which approach is actually the right. So any ideas or links please.
ASKER CERTIFIED SOLUTION
Avatar of SSupreme
SSupreme
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fajer39
Fajer39

ASKER

Thanks SSupreme.
Just stumbled across this Q&A and I think it deserves a better answer.  Using a REGEX to validate an email address is the sort of thing we might have done a decade ago - nobody does something like that any more.  Today we use the built-in PHP filters.  Here are the links to the relevant man pages.
http://php.net/manual/en/function.filter-var.php
http://php.net/manual/en/filter.filters.validate.php
http://php.net/manual/en/filter.filters.sanitize.php

This Q&A brings up two issues that deserve attention.  First, check the age of sources and references.  Many things on the W3Schools site are still useful, but this is not one of them.  It's literally several years out of date!  Please don't propagate obsolete code examples when there are modern solutions we can choose instead.

Second, to Fajer39, you might want to consider leaving your E-E questions open for at least 24 hours.  The E-E community is all volunteers, and we often do not see questions immediately (some of us check in at the end of the workday, for example).  If you leave the questions open for a day, you will undoubtedly get many more sets of eyes on the problem!
Ray, I cannot see any newer information provided from your links compared to mine.  "filter_var" exist since 2006 when 5.2 was released and even if W3Schools out of date, may be you have your own up to date resource?!

Ray, you can say that REGEX was used by dinosaurs but http://php.net/manual/ru/function.preg-match.php still exists and if you want live validation you would probably use REGEX.

Please don't spread false information, to confuse author.
If you feel that any part of the information I shared is "false" please point it out, thanks.
Today we use the built-in PHP filters
from link provided by me http://www.w3schools.com/php/php_form_url_email.asp you can see following:
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format";
}

Open in new window

Clear usage example of PHP filter for email as requested in question.
Yet, you state:
Many things on the W3Schools site are still useful, but this is not one of them.  It's literally several years out of date!
, it is a contradiction.
Using a REGEX to validate an email address is the sort of thing we might have done a decade ago - nobody does something like that any more.
Completely wrong statement! Gmail still use, for example, I guess, when somebody typed wrong symbol at registration, it suggests: (see image), how? REGEX! NOT PHP filter!  Also you can google for live validation, and most of the time is REGEX.
And the video about REGEX from EE submitted on 2014-08-12 and updated in May this year. All of that means, at least some people still use it.

More over, personally, I would never suggest using PHP filter for email acceptance because it stops correct emails to system, at the end of the day, humans know better what they want to writer and system can provide a hint or make an attention.
Capture.JPG