N
N
Natalia2017-01-21 20:36:11
PHP
Natalia, 2017-01-21 20:36:11

How to properly validate the email field in PHP in a form?

Hello everybody!
Please help me figure out the correct email validation.
There is an HTML form (the example shows part of the email form):

<label>E-mail:</label>
    <input id="input5" type="text" size="25" name="email" value= "<?php echo $email ?>">
    <span class="error">* <?php if(isset($errors['email'])) { echo $errors['email']; }?></span>
    <br><br>

, которая обрабатывается PHP.
// пример обработки поля email. другие поля обрабатываются таким же образом:
... 
if (empty($_POST["email"])) {
    $errors['email'] = "Не заполнено обязательное поле";
  } else {
    $email = test_input($_POST["email"]);
  }
...

I understand that you need to use FILTER_VALIDATE_EMAIL , but where exactly do you need to enter the "sample" according to which the data entered in the form will be further compared?
There is an idea to formalize the general approach to email validation as follows:
Option 1:
If something was entered in this form field if(isset($_POST["email"])), then we begin to check for the correctness of the entered data, namely:
use if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)).
If the entered data is correct (that is, it corresponds to the initially specified template, for example $var = "[email protected]";), then we continue working on other form fields.
A If the entered data is incorrect, then we send the value to the error array ($errors) and display a warning about it ( $errors['email'] = "Not a valid email";). And if no data has been entered, then the "empty" value is also sent to the same error array ($errors) , it has its own message already set ($errors['email'] = "Required field not filled";).
Option 2: If empty
was sentemail field, then we assign the received value to the error array and issue the message "Required field not filled" (as it is in part of the code presented), otherwise, if the email field was not empty (some data was entered), then we start checking : If the entered data is incorrect (that is, it matches the initially specified template, for example $var = "[email protected]";), then the result is sent to the error array with the message "Not a valid email". Otherwise (if the result obtained is correct), then we continue to move on to check other forms.
The most important question:
where in the code is it necessary to set this pattern/sample ($var = "[email protected]"), which will be used to compare the entered mail for its correctness; so that everything works as it should, and so that this pattern does not pop up anywhere in the browser?
Thank you in advance for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Natalia, 2017-01-21
@nlynx

For those interested, here is the solution:

if (empty($_POST["email"])) {
    $errors['email'] = "Не заполнено обязательное поле";
  } elseif ( filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) { 
    $errors['email'] = "формат почтового ящика неправильный";
  } else {
    $email = test_input($_POST["email"]);
  }

It was necessary to remove the negation before filter_var($_POST["email"]... etc.
And you don't need to set a pattern / sample, as was the case in w3school.
Everything works as it should.

A
Anton Tikhomirov, 2017-01-21
@Acuna

And this is yours, excuse me, what kind of sample is this? Where does it come from and how do you plan to check something on it? You need to check the regular expression already when submitting the form. That is, when the user clicked the "Submit" button on the form.

$errors = [];
if (!preg_match ('/[\.a-z0-9_\-]+[@][a-z0-9_\-]+([.][a-z0-9_\-]+)+[a-z]{1,4}/i', $_POST['email']))
$errors[] = 'Не валидный email';

if ($errors) {
  
  $content = 'При отправке формы произошли следующие ошибки:<br/><br/>';
  foreach ($errors as $error) $content .= $error.'<br/>';
  echo $content;
  
}

Ideally, you need to submit the form in Ajax and display a message about an incorrectly entered address right below the input field, beautifully designed without reloading the page. But you need to have other skills.

F
fenric, 2017-08-04
@fenric

https://anatoly.fenric.ru/development/validacia-em...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question