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.
PHPJavaScriptEmail Servers

Avatar of undefined
Last Comment
SSupreme

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
SSupreme

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SSupreme

Fajer39

ASKER
Thanks SSupreme.
Ray Paseur

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!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SSupreme

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.
Ray Paseur

If you feel that any part of the information I shared is "false" please point it out, thanks.
SSupreme

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.