Using onsubmit with external js file

Hi! I’m using onsubmit in my form tag to call javascript functions for form validation. I have no problem if I include the functions in the same file as the form but would prefer to have the functions in an external file. The latter I’ve been unable to do despite the fact that the external file is properly linked (other functions in that file can be called without problem).

This is the syntax I’ve used thus far (firstName and lastName are input ids):

<form action=“URL”
onsubmit=“return (
functionName(this.firstName, otherArguments)
&&
functionName(this.lastName, otherArguments)
&&
.
.
.
);”>

Again, it works if the functions are in the same file as the form but not if they’re in an external file.

Sorry if there’s something basic/silly/obvious I’m overlooking. If there’s some place on this site to which I could have gone to get this info, please let me know!

Thanks!
James

It should work fine. I think there’s other differences besides what’s obvious from your post. You might consider fleshing out a more complete example for us.

Try something like this:

<form id=“test”>
blablabla
</form>

Then External JS File:

var FormValidation = document.GetElementById(“test”).onClick = function(){

//Form Validation here

}

window.onload = FormValidation;

Hi,

Sorry I didn’t get back to this sooner. I like to figure things out for myself when I can. :slight_smile:

I resolved the problem by altering the file extension of the external file from .js to .php.

I could have sworn .js was a legit file extension that could be used for JS files. Could it be that my Apache installation disallows use of .js files?

Thanks for your responses and sorry to have wasted time.
James