I want to refer to this post, because it might relate:
Make Form Fields Optional with JavaScript Validation
I have a form with three optional fields, as described above. If I click the submit button, the JavaScript alerts come up, but the last one is a URL instead of the string I specify in the JavaScript function (the one that isn’t an alert strong but an URL).
After a second, the page tries to go to an invalid URL:
localhost.../index.php/Don%27t%20forget%20the%20location.
As it turns out the Don%27t%20forget%20the%20location. is the alert string I have in the JavaScript function.
I thought that I might have some weird code that I accidentally pasted somewhere causing this but I scoured my files and found nothing out of the ordinary that would cause this. Not sure if this is a bug or something I’m doing wrong.
EDIT
I have JavaScript form validation functions like so:
function validate_name(field)
{
if (field == "") return "Please enter the name.\n";
return "";
}
function validate_specialty(field)
{
if (field == "") return "Please enter the specialty.\n";
return "";
}
function validate_location(field)
{
if (field == "") return "Don't forget the location.\n";
return "";
}
where the function that is called from the form’s onSubmit is:
function validate_form(form)
{
name = validate_name(form.name.value);
specialty = validate_specialty(form.specialty.value);
location = validate_location(form.location.value);
if (name == "" || specialty == "" || location == "")
{
return true;
}
else
{
alert("You must enter at least one field:\n\n" + name + specialty + location);
return false;
}
}
It’s because the variable
locationrefers towindow.locationin that case (the url). So if you change your variable name, that should work:and