I have a form with some inputs, and I want to make an js validation for that form. But, preventDefault
is not working for me.
HTML Code
<form action="" id="form">
<h2>Send us a message</h2>
<div class="input-box" id="nameInputBox">
<small class="errorMsg-hidden">error msg</small>
<input type="text" name="" required id="name">
<span>Name</span>
</div>
<div class="input-box">
<input type="submit" name="" value="Nosūtīt" id="submit">
</div>
</form>
JS Code
const form = document.getElementById('form');
const name = document.getElementById('name');
form.addEventListener('submit', (e) => {
e.preventDefault();
});
In this case, it is important to keep the required attribute in html, because some of styling depends of :valid
pseudo class.
Is there any solution here?