Design comparison
Solution retrospective
i completed the validation of the form, and then when every input are filled, the alert show a message saying that it is completed, but i used the preventDefault to prevent the form being submited, and i dont know how can i stop the preventDefault from working after the inputs are completed, so the form isnt sending nothing, can someone help me ?
Community feedback
- @Grego14Posted 5 months ago
👋 Hello! 🎉 Congratulations on completing the challenge! 🎉
The preventDefault() method is used in a form when you don't want it to be sent, in this case you want it to be sent when everything is correct.
To prevent the form from being submitted when there is an error, you can use the preventDefault() along with validations, something like this:
nameValidate(); lastNameValidate(); emailValidate(); radioValidate(); checkBoxValidate(); if (!(checkBox.checked === true) && !(fields.value.length > 0) && !(fields.value.length > 0) && !(emailRegex.test(fields.value)) && !(htmlRadio1.checked || cssRadio2.checked)) { return e.preventDefault() } alert("Message Sent. Thanks for completing the form. We'll be in touch soon!");
Here we would be using all the validation functions before verifying that the form is correct, since those functions remove and add the errors.
And we use
return e.preventDefault()
in validating whether the inputs are invalid to prevent the alert from being sent.Using the tag
br
is considered a bad practice, as it should be used only when breaking the line is necessary, such as a poem of a few lines. It's best to use styles to achieve what you want.When using multiple inputs in a container, it's best to use the 👉 Fieldset tag instead of a div.
You have duplicated ids in two input elements lname.
No need to use the type attribute in the
script
tagI hope this helps! 😁
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord