...

View Full Version : Javascript Form Validation Error Appearing



nevblanc79
07-11-2011, 12:50 PM
Hi,

I have placed a form validation script in my HTML and I think its all in the right place, however, when i load the page an error appears saying can not get object. Once I click past it, the form validation works as normal. Not sure how to get rid of this error.

I have placed the link to the external validation script in the document head

Any ideas?

HTML
<div class="mb_content">
<h2>Work</h2>
<div class="mb_content_inner">
<p>IMAGES COMING SOON.</p>
</div>
</div>
<div class="mb_content">
<h2>Contact</h2>
<p class="con_details_tel">tel: </p>
<p class="con_details_email">email: </p>
<div class="mb_content_inner">
<form id='contactus' name='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>
<fieldset >
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
<input type='text' class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />

<div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
<div class='container'>
<label for='name' >Name: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='email' >Email:</label><br/>
<input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
<span id='contactus_email_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='message' >Message:</label><br/>
<span id='contactus_message_errorloc' class='error'></span>
<textarea name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
</div>

<div class='container'>
<input type='submit' name='Submit' value='Submit' />
</div>
</fieldset>
</form>
<script type='text/javascript'>


var frmvalidator = new Validator('contactus');
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");

frmvalidator.addValidation("email","req","Please provide your email address");

frmvalidator.addValidation("email","email","Please provide a valid email address");

frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");


</script>

</div>
</div>
</div>

Here is the start of the javascript that is producing the error:-


function Validator(frmname)
{
this.validate_on_killfocus = false;
this.formobj = document.forms[frmname];
if (!this.formobj)
{
alert("Error: couldnot get Form object " + frmname);
return;
}

Can anyone help?

Thanks

nevblanc79

Kor
07-11-2011, 01:10 PM
Post the HTML code (not the PHP code, as we can not run a php code on our localhost without the PHP variables defined). Or post a link to a test page.

By the way: what error? A "can not get object" is not sufficient (and probably not accurate).

nevblanc79
07-11-2011, 01:19 PM
Have removed the php from the code.

With regard to the error, when the page loads an alert box appears with the text ("Error: couldnot get Form object contactus)

Thanks

Niall


<div class="mb_content">
<h2>Work</h2>
<div class="mb_content_inner">
<p>IMAGES COMING SOON.</p>
</div>
</div>
<div class="mb_content">
<h2>Contact</h2>
<p class="con_details_tel">tel:</p>
<p class="con_details_email">email:</p>
<div class="mb_content_inner">
<form id='contactus' name='contactus' action='' method='post' accept-charset='UTF-8'>
<fieldset >
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='' value='/>
<input type='text' class='spmhidip' name='' />

<div><span class='error'></span></div>
<div class='container'>
<label for='name' >Name: </label><br/>
<input type='text' name='name' id='name' value='' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='email' >Email:</label><br/>
<input type='text' name='email' id='email' value='' maxlength="50" /><br/>
<span id='contactus_email_errorloc' class='error'></span>
</div>

<div class='container'>
<label for='message' >Message:</label><br/>
<span id='contactus_message_errorloc' class='error'></span>
<textarea name='message' id='message'><></textarea>
</div>

<div class='container'>
<input type='submit' name='Submit' value='Submit' />
</div>
</fieldset>
</form>
<script type='text/javascript'>


var frmvalidator = new Validator('contactus');
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("name","req","Please provide your name");

frmvalidator.addValidation("email","req","Please provide your email address");

frmvalidator.addValidation("email","email","Please provide a valid email address");

frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");


</script>

</div>
</div>
</div>

Kor
07-11-2011, 01:52 PM
HTML typo: you missed a quote:


<input type='hidden' name='' value='/>


My advice is to use always double quotes to nest the attributes' values:


<input type="hidden" name="" value="" />
// and so on


And, in the fragment the code you have posted, you have an extra </div> end tag. Be careful. HTML errors might bring JavaScript and CSS errors as well.

nevblanc79
07-11-2011, 02:25 PM
Hi.

Have added double quotes around all attributes but I still get the error message when the page loads.

nevblanc79
07-11-2011, 03:34 PM
Do you need the javascript file as well?

Kor
07-11-2011, 03:44 PM
No... All I can see is that you code does not see the form element. Which is weird, because when I simplified the code and run it locally, that worked. I still suspect that there is an error somewhere else, but you have so many other external codes, that I am not able to sense where... So far.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum