I'm currently making a html web page for my course where the user inputs some data in a form which is then meant to be validated via a separate javascript file which gives an alert with the errors. This is my javascript code currently:
function init() {
var rForm = document.getElementById('regform');
rForm.onSubmit = validate();
}
window.onload = init;
So it's meant to get the form with the id 'regform' and then when that form is submitted, call the validation function. However as soon as the web page loads, it calls the validation function and gives the errors and when the form is submitted is does not call the function which is meant to stop the form from submitting. The validation code works as I've tested it by placing the code in the html file, but I have to put the code in a separate javascript file.
So for some reason it's skipping the rForm.onSubmit
and going straight to calling validate()
. I am new to javascript so it may be something really obvious.