On my website, I have a form that requests the user's personal information. After filling out the form, a modal pops up with a "Thank you for signing up" message. The issue is that even if all fields are left blank and the button is clicked, the modal still appears. How can I make the modal appear only after all fields are completed?
I also need the user's input to remain on the page after clicking the button, but changing the button type from submit to button causes all input fields to clear as soon as the last question is answered.
If anyone has suggestions or solutions, it would be greatly appreciated!
Below is the form:
<form id="personalinfo" name="personalinfo" onsubmit="ask()">
<b>Please sign up with us before you proceed:</b><br>
First Name: <input name="firstname" required="" size="40" type="text"><br>
<br>
Last Name: <input name="lastname" required="" size="40" type="text"><br>
<br>
Age: <select name="dropdown">
<option value="1">10-18</option>
<option value="2">19-25</option>
<option value="3">26-35</option>
<option value="4">36-45</option>
<option value="5">46-55</option>
<option value="6">56-65</option>
<option value="6">65+</option>
</select><br>
<br>
Gender: <input name="gender" required="" type="radio" value="male"> Male<br>
<input name="gender" required="" type="radio" value="female"> Female<br>
<input name="gender" required="" type="radio" value="other"> Other<br>
<br>
<br>
<button id="OK!">OK!</button>
</form>
<div id="myModal" class="modal" type="button">
<div class="modal-content">
<span class="close">×</span>
<p>Thank you for signing up with us ! You can now proceed to the test.</p>
</div>
</div>
<script>
var modal = document.getElementById('myModal');
var btn = document.getElementById("OK!");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function()
{
modal.style.display = "block";
}
span.onclick = function()
{
modal.style.display = "none";
}
window.onclick = function(event)
{
if (event.target == modal)
{
modal.style.display = "none";
}
}
</script>
</form>