Reputation: 2425
I'm writing a javascript snippet that will validate the signup form fields. for some reason, the onsubmit function, is also called when the page loads.
window.onload = function validate_signup_form() {
var form_element = document.getElementById("signup_form");
var onload_values = document.forms["signup_form"];
var test = onload_values.elements[0].value;
var required_fields = new Array();
document.getElementById("signup_form").onsubmit = check_values();
function check_values()
{
var current_values = document.forms["signup_form"];
for (x in current_values)
{
if (current_values[x].value == onload_values[x].value)
{
// Error: The form values match the initial ones
return false;
}
elseif (current_values[x].value.toLowerCase() == onload_values[x].value.toLowerCase())
{
// Error: The form values match the initial ones
return false;
}
elseif (current_values[x].value.toUpperCase() == onload_values[x].value.toUpperCase())
{
// Error: The form values match the initial ones
return false;
}
}
};
};
Shouldn't check_values(); run only on submit ?
Upvotes: 1
Views: 55
Reputation: 1719
You are setting the onsubmit property to the value returned by check_values(); Change to the following:
document.getElementById("signup_form").onsubmit = check_values;
Upvotes: 2
Reputation: 5706
In the following line you are calling check_values
and setting signup_form's onsubmit
to whatever the function returns.
document.getElementById("signup_form").onsubmit = check_values();
Remove the brackets if you want to set the function itself.
Upvotes: 1
Reputation: 15351
Same problem as in this question asked not long ago.
You are assigning the return value of the chekc_values
to onsubmit
and not the function itself. It should be onsubmit = check_values;
.
check_values
is executed at the time of assignment only not when the event is fired.
Upvotes: 1