1

Temat: Walidacja formularzy

https://ti.dxe.pl/files3/form4.jpg

2

Odp: Walidacja formularzy

<script>
    function funkcja()
    {
        var imie = document.getElementById('imie');
        var nazwisko = document.getElementById('nazwisko');
        var telefon = document.getElementById('telefon');
        var email = document.getElementById('email');
        var regulamin = document.getElementById('regulamin');
        var wynik = document.getElementById('wynik');
        //
        const regEmail = /\S+@\S+\.\S+/; //wzór email
        //
        var B = [];
        // imie
        if (imie.value.length<=0) B.push("Wypełnij poprawnie pole z imieniem.");
        // nazwisko
        if (nazwisko.value.length<=0) B.push("Wypełnij poprawnie pole z nazwiskiem.");
        // telefon
        if (telefon.value.length!=9) B.push("Wypełnij poprawnie pole z telefonem.");
        // email
        if (!regEmail.test(email.value)) B.push("Wypełnij poprawnie pole email.");
        // regulamin
        if (!regulamin.checked) B.push("Zaakceptuj regulamin.");
        if (B.length==0)
          wynik.innerHTML = "Formularz prawidłowy.";
        else
          wynik.innerHTML = B;
    }
</script>

<h1 style="margin: 100px;">
<form>
    <span id="wynik" style="color: blue;">Wypełnij formularz</span>
    <hr>
    Imię:<br>
    <input type="text" value="" id="imie"><br>
    Nazwisko:<br>
    <input type="text" value="" id="nazwisko"><br>
    Telefon (np. 123456789):<br>
    <input type="text" value="" id="telefon"><br>
    E-mail:<br>
    <input type="text" value="" id="email"><br>
    <input type="checkbox" value="" id="regulamin"> Akceptuje regulamin<br>
    <hr>
    <input type="button" value="Sprawdź" onclick="funkcja()">
</form>
</h1>