I saw another post by someone who seemed to be having a similar problem, but I didn’t find a resolution there, so here goes …
I have a php-created page containing a form. I want the form submit button to run a javascript function which will validate the form data. The form itself is written directly in html (there’s very little dynamic part of it, although it is contained in a php file).
I have gotten the validate function so that it works fine when embedded in the html code in the above-mentioned php file. But for some reason, as soon as I move it (i.e. copy it verbatim) from there to the separate .js file, it stops working.
Here is the validation function:
[QUOTE]
function validate(work, answer) {
if (work == “” || work == null) {
alert (“Please show your work in the box on the left.”) ;
return false;
} else if (answer == “” || answer == null) {
alert (“Please write your final answer in the box on the right.”) ;
return false;
} else if (isNaN(answer)) {
alert(“Your final answer should be a number.”
return false;
} else {
return true;
}
}
Here is code that is supposed to call the .js file where the above code is located (this seems to work fine because it works when I put some other functions – but not the above one – into the .js file and then run them):
[QUOTE]
<script type=”text/javascript” src=”form_validation.js”></script>
Here is a snippet of the form that actually uses the validation function:
[QUOTE]
<form id=amt_contform method=POST action=<?php echo $next_ques_link ; ?> onSubmit=”return validate(ques1work.value, ques1answer.value);” >
<p>Show your work here.</p>
<textarea name=ques1work rows=25 cols=40></textarea>
<p>Write your final answer here.</p>
<textarea name=ques1answer id=ques1answer rows=1 cols=20></textarea>
<p>Click "Next" when you are finished.</p>
<input id=SubmitButton type=submit name=Next value=Next >
</form>