I have a simple form with fields that I am trying to validate using JavaScript. In the form I have an input type of submit.
For validation, should I call the validation function for onclick event on the input or onsubmit of the form? Is there any implication of choosing one over the other one?
OnSubmit is used on a form , and indicates that the information is to be submitted to the server at this point unless you return false. OnClick is used on anything, and indicates that it was clicked, offering no other context to the intention of the event at all.
The onsubmit event is an event that occurs when you try to submit a form. You can put your form validation against this event type. The following example shows how to use onsubmit. Here we are calling a validate() function before submitting a form data to the web server.
The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.
The submit event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript. The method form. submit() allows to initiate form sending from JavaScript.
onsubmit
is triggered whenever the form is about to be submitted.onclick
is triggered when the specific button is clicked.
Forms can be submitted by hitting the enter key in any input field. This would not trigger the onclick
of the submit button, but it would trigger the form submit event. As such, use onsubmit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With