How to require text in form

Hi. I want to include that the user must write something in both forms to submit. For some reason required is not working. Can you point me where the mistake is?

    <form>
        <div class="formBox">
            <label for="name">Име</label>
            <input type="text" id="name" placeholder="Име" required/>
        </div>
        <div class="formBox">
            <label for="surname">Презиме</label>
            <input type="text" id="surname" placeholder="Презиме" required/>
        </div>
        <div class="formBox">
            <button id="btn" >Започни тест</button>
        </div>
    </form>

js file

let infs = [];

        const addInf= (ev)=>{

            ev.preventDefault();  //to stop the form submitting

            let inf = {

                id: Date.now(),

                name: document.getElementById('name').value,

                surname: document.getElementById('surname').value

            }

            infs.push(inf);

     

            //saving to LS

            localStorage.setItem('MyInfList', JSON.stringify(infs) );

            document.forms[0].reset();                  // to clear the form for the next entries

            window.location = "index7.html";

        }

        document.addEventListener('DOMContentLoaded', ()=>{

            document.getElementById('btn').addEventListener('click', addInf);

        });

What browser are you using?

2 Likes

Hi emilijastt, welcome to the forums!

In desktop browsers the code you’ve posted does indeed require inputs to siubmit!

Edit)
Beaten to the punch by @Gandalf. :slightly_smiling_face:

2 Likes

Chrome

Hi, thanks! :smile: Could you tell me more about that input, because I would need to change more of my code. I have inserted the js part. Thank you for your time :heart:

When I test it works in Linux Chromium, the one Chrome is built from.

On what platform does it not work?

Then I suggest the topic to move to the Javascript department, do you agree?

It also works for me Chrome 81 on Windows.

It’s Chrome 81 but it doesn’t work, also no new updates.

Chrome 81. Sure :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.