/    Sign up×
Community /Pin to ProfileBookmark

more validation issues

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;
}
}

[/QUOTE]

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>

[/QUOTE]

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 &quot;Next&quot; when you are finished.</p>
<input id=SubmitButton type=submit name=Next value=Next >

</form>

[/QUOTE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 12.2010 — alert("Your final answer should be a number."
I'm sure you'll see the problem with that. It will save you a lot of hassle if you check the error console when things don't work.

Also, I would strongly recommend that you quote your attributes and use a doctype, I recommend HTML 4.01. You don't have to, but it's recommended.
Copy linkTweet thisAlerts:
@baixiweiauthorJul 12.2010 — Thank you!!

I have just been writing my scripts in Notepad, and they are run on a third-party site, so I haven't been seeing my error results. I guess I need to figure out a way to see them from now on.

Thanks for the advice about attributes and doctype. I don't really know what it means, but I'm sure I can figure it out.
×

Success!

Help @baixiwei spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...