/    Sign up×
Community /Pin to ProfileBookmark

form validation.

i think my problem is simple to solve but i am a beginner to javascript but not coding.

[code]<script type=”text/javascript”>
<!–
function validateanswer(form)
{
if (form.answer.value == “”)
{ alert (“Please enter an answer!”);
form.answer.focus();
return false;
}

return true;
}
//–>
</script>[/code]

So here is my javascript code between the head tags. this should alert me if my ‘answer’ field is empty. simple enough but it doesnt.

[code] onsubmit=”return validateanswer(form);”[/code]

I have this in my <form> tag before the >. so on submit the script should run. ok. my form action takes me to another page. does that matter? I dont think it does.

What is wrong here?

form.answer.value, the word ‘form’ here does this have to resemble my form name? I took this coding from a book but it doesnt seem to work. the alert doesnt execute.

I have some php code in the file however it doesnt interfere.

yes i do have js enabled.

thanks in advance. help me debug.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @poofacer 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 5.18,
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,
)...