0

Making a school assignment where I have to make a CRUD app containing members. Part of the assignment is as follows:

  • each member can has many (unknown) number of email adresses
  • I have to use HTML to validate if each of those is an email adres
  • I CAN'T use JS or any other language beside PHP and HTML (and SQL)

The things I tried (and the teacher said they are not correct):

  • Use the 'multiple' attribute on a input field (he doesn't want me to use 'multiple')
  • Use a textarea and seperate each email on a new line (can't use regex)

I am really stuck here.. anyone has any idea on how I can approach this in a way that I can upload an unknown number of email adresses (upon creating a member) and still have each be validated?

Xavvey
  • 43
  • 1
  • 1
  • 5

1 Answers1

0

Solution 1

Well, you can indeed use a textarea, split the posted value into an array, trim the lines, validate that each line contains a valid E-Mail address and do whatever you would like with the mails:

<form method="POST">
    <p>
        <label for="emails">
            One E-Mail per line:
        </label>
        <textarea name="emails" id="emails" cols="80" rows="10">jdsjdhs@sdhjsdhj.de
jsddhs@ssdddhjsdhj.email
jsddhs@sdsddhj.tk
jdsjdsdhs@sdhjsdhj.org
jdsjdsdhs@sdhjsdhj.jetzt</textarea>
    </p>
    <p>
        <input type="submit" value="Validate">
    </p>
</form>

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $emails = explode("\n", $_POST['emails']);

    $emails = array_map('trim', $emails);

    $emails = array_filter($emails, function ($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    });

    $validEmailCount = count($emails);

    echo <<<HTML
    <p>
        $validEmailCount valid E-Mails found.
    </p>
    HTML;

    // Your database logic here
}

Solution 2

Based on the below comment thread, the OP is looking for an HTML only email "validation".

For that purpose, the input[type=email] is probably the best choice.

To allow as many emails as wanted, I have written the below PHP, which adds a blank email field every time the form is submitted with the checkbox asking for another field is marked as on.

<?php

$emails = [''];

if (
    $_SERVER['REQUEST_METHOD'] === 'POST' &&
    isset($_POST['emails']) &&
    is_array($_POST['emails'])
) {
    $emails = $_POST['emails'];

    if (
        isset($_POST['add_mail']) &&
        $_POST['add_mail'] === 'on'
    ) {
        $emails[] = '';
    } else {
        print_r($emails);
        die;
    }
}

?>

<form method="POST">
    <?php foreach ($emails as $email) : ?>
        <p>
            <input type="email" name="emails[]" value="<?= $email ?>">
        </p>
    <?php endforeach; ?>

    <p>
        <label for="add_mail">
            Would like to enter one more E-Mail?
        </label>
        <input type="checkbox" name="add_mail" id="add_mail">
    </p>

    <p>
        <input type="submit" value="Send">
    </p>
</form>

Solution 3

Another solution would be to use a single input[type=text] in combination with the [pattern] attribute, and write a regular expression for it, that allows multiple email addresses.

<input type="text" pattern="…" required>

The emails could be separated by space, commas or semicolons, for example.

You would then do something like ,? to allow a delimiter or not.

David Wolf
  • 1,400
  • 1
  • 9
  • 18
  • Thanks for the idea. I have thought of this but the thing is that my teacher says I have to use HTML to validate them.. I have asked other people and they are all stuck on this as to how to do it... – Xavvey Feb 10 '22 at 20:51
  • You can't do validation in HTML without using JavaScript. HTML is not a programming language. – Barmar Feb 10 '22 at 21:01
  • I might be using the wrong words.. sorry for that. If I translate his feedback then it says: "You shouldn't be able to add "dave" as an email. Simple test the input using HTML'. Validation might be the wrong word, but he wants me to use HTML to check if it really is an email or not – Xavvey Feb 10 '22 at 21:03
  • @Xavvey I added another solution approach to my question, see _Solution 2_. – David Wolf Feb 10 '22 at 21:27
  • Thanks! this seems like it will do the trick (I hope because I don't know what else to do...) – Xavvey Feb 10 '22 at 21:51