Check required fields | length is null or not an object

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Check required fields | length is null or not an object

Post by Sindarin »

While I am trying to check client-side a form with javascript, everything works except "length" throws up an error "length is null or not an object".
Here is my script,

Code: Select all

/* CHECK REQUIRED FIELDS */
 
function submitForm()
{
 
//define error message number and target successful fields number
var error_messages = new Array(2);
var fields_successful = 2;
var fields_successful_target = fields_successful;
 
 
//define error header and footer
var error_header="<center><div style='background-color:#FFFF66;width:390px;height:20px;text-indent:4px;padding:3px;'>[!] ";
var error_footer="</div><br/></center>";
 
//get required fields by ID
var alertdiv = document.getElementById("alertdiv");
var field1 = document.getElementById("first");
var field2 = document.getElementById("last");
 
field1.style.background="#FFFFFF";
field2.style.background="#FFFFFF";
 
//clear div
alertdiv.innerHTML="";
 
//name must be completed
if (field1.value=="")
{
error_messages[0]=error_header+"Name is a required field."+error_footer;
field1.style.background="#FFFF66";
fields_successful = fields_successful-1;
window.scrollTo(0,0);
}
else
{
//clear error
error_messages[0]="";
}
 
//name must be completed
if (field2.value=="")
{
error_messages[1]=error_header+"Surname is a required field."+error_footer;
field2.style.background="#FFFF66";
fields_successful = fields_successful-1;
window.scrollTo(0,0);
}
else
{
//clear error
error_messages[1]="";
}
 
//display errors if any
for(var i=0;i<error_messages[i].length;i++){
alertdiv.innerHTML=alertdiv.innerHTML+error_messages[i];
}
 
//all fields ok - submit form
if (fields_successful==fields_successful_target)
{
alertdiv.innerHTML = "";
document.contactform.submit();
}
 
}
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Check required fields | length is null or not an object

Post by mintedjo »

I think you meant error_messages.length rather than error_messages.length
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Check required fields | length is null or not an object

Post by Sindarin »

I think you meant error_messages.length rather than error_messages.length


If I do that, I get no errors but I get an "undefined" string popping up in the div.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Check required fields | length is null or not an object

Post by mintedjo »

I don't see how :-)
Can you post your new code?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Check required fields | length is null or not an object

Post by Sindarin »

Duh, it was the array, I used 1 required field while I set the array length to 2. So the second value in the array was truly undefined. :D
Post Reply