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 ?
You are setting the onsubmit property to the value returned by check_values(); Change to the following: