...

View Full Version : Simple Validation



Donno24
06-19-2008, 12:41 PM
Hi everyone,

I am using this code to validate a form with 3 questions each question needs to be correct for the complete form to process.

Could someone please look at this script and tell me why its not working?

Many thanks
Donno


<script language="JavaScript">
function checkform()
{
if (Question1.value == 'Correct Answer')
{
return false;
}
else if (Question2.value == 'Correct Answer')
{
return false;
}
else if (Question3.value == 'Correct Answer')
{
return false;
}

return true;
}
</script>

rangana
06-19-2008, 12:59 PM
You should append document and the name of your form.


if (document.formName.Question1.value == 'Correct Answer')


If that ain't help, show us your markups (HTML).

Donno24
06-19-2008, 01:10 PM
I have cut peices out as these are the only feilds I require to be validated.

Thanks
Donno
JAVASCRIPT

function checkform()
{
if (document.submitter.Question1.value == 'Corrent Answer')
{
return false;
}
else if (document.submitter.Question2.value == 'Corrent Answer')
{
return false;
}
else if (document.submitter.Question3.value == 'Corrent Answer')
{
return false;
}

return true;
}


HTML


<form method="post" action="" onsubmit="return checkform()" name="submitter">
<table border="0" class="formPage">

<tr>
<td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
<td class="field">
<input type="text" name="question1" size="40" maxlength="255" class="text" />
<br />
</td>
</tr>
<tr>
<td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
<td class="field">
<input type="text" name="question2" size="40" maxlength="255" class="text" />
<br /></td>
</tr>
<tr>
<td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
<td class="field">
<input type="text" name="question3" size="40" maxlength="255" class="text" />
<br /></td>
</tr>
<td colspan="2" class="buttons"><input type="submit" name="submit" value="{l}Continue{/l}" class="btn" /></td>
</tr>
</table>
</form>

Philip M
06-19-2008, 01:27 PM
I have cut peices out as these are the only feilds I require to be validated.


The rule of spelling is i before e, except after c. :thumbsup:

and to be really pedantic - append means add after, prepend means add before. :eek::thumbsup:

rangana
06-19-2008, 01:32 PM
JS is case-sensitive. Capitalization matters.


Question1.value
Question2.value
Question3.value


Highlighted should be in small letters.

BTW, language is a deprecated attribute use:


type="text/javascript"

Hope it helps.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum