Simple form validation not working!!!?

F

flagman5

Hi All

I have this simple code,

<SCRIPT TYPE="text/javascript">
<!--
function validateOnSubmit() {

valid = true;
if(document.pollname.question.value == "")
{
document.getElementById('in_question').innerHTML = 'Required!
Please put something';
valid = false;
}
return valid;

};
//-->
</script>

to check the input fields, make sure they are not empty. and if empty,
simply change a field next to it to a different text. the HTML form
code is

<form action="member_polls.php" method = "POST" name="pollname"
onsubmit="return validateOnSubmit();">

<input type="text" name="question" size="60"><td id="in_question">
Required</td><br/><br/>

So, my problem is, the form does NOT stop processing! I dont
understand? It is suppose to return a false if the field is empty, but
when i try it out, the form continues processing. What gives?

Thanks in advance
 
A

ASM

(e-mail address removed) a écrit :
I have this simple code,

<SCRIPT TYPE="text/javascript">
<!--
function validateOnSubmit() {

valid = true;
if(document.pollname.question.value == "")
{
document.getElementById('in_question').innerHTML = 'Required!
Please put something';

as I am not sure you scripted that on an alone line :

var str = 'Required! Please put something';
document.getElementById('in_question').innerHTML = str;
valid = false;
}
return valid;

};
//-->
</script>

<SCRIPT TYPE="text/javascript">
function validateOnSubmit() {
if(document.pollname.question.value == "")
{
var str = 'Required! Please put something';
document.getElementById('in_question').innerHTML = str;
return false;
}
return true;
}
<form action="member_polls.php" method = "POST" name="pollname"
onsubmit="return validateOnSubmit();">
<input type="text" name="question" size="60"><td id="in_question">
Required</td><br/><br/>

what do all these BR between TD ??
So, my problem is, the form does NOT stop processing!

a variable must be set in one line ... no ?
 
F

flagman5

Thanks for reply, but the problem persists

My main issue is that the form should receive a false from the
onsubmit call to the js function and cancel the submission. BUT the
javascript false does not stop the process and i proceed into the next
page as defined in the form action.

what do you mean a variable must be set in one line? i have everything
on the same line?
 
R

RobG

Thanks for reply, but the problem persists

Please don't top-post, reply below trimmed quotes.
My main issue is that the form should receive a false from the
onsubmit call to the js function and cancel the submission. BUT the
javascript false does not stop the process and i proceed into the next
page as defined in the form action.

Your problem is probably related to invalid HTML, which is creating a
DOM different to what you expect.
what do you mean a variable must be set in one line? i have everything
on the same line?

When posting code, use 2 spaces for indents and manually wrap code at
about 70 characters. Otherwise, newsreaders will likely insert line
breaks that make it much harder to evaluate your code and often
introduce errors. Someone should be able to just cut and paste your
code into a page and see it "work".

So here's your example, modified as suggested above:

<script type="text/javascript">
function validateOnSubmit() {
valid = true;
if(document.pollname.question.value == "") {
document.getElementById('in_question').innerHTML =
'Required! Please put something';
valid = false;
}
return valid;
}
</script>

<form action="#" name="pollname"
onsubmit="return validateOnSubmit();">
<div>
<input type="text" name="question" size="60">
<span id="in_question">Required</span>
<br>
<input type="submit">
</div>
</form>

The code is almost identical to what you posted with linebreaks
removed. All I did was provide valid HTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top