Click to See Complete Forum and Search --> : Form validation form.length


braveheartkenya
October 4th, 2006, 09:46 PM
Greetings all!

I'm quite new to javascript so i may have some rather silly kind of questions... but anyway here's the problem...

I want to loop through all the elements in a form so as to check their values in order to validate the relevant fields, i thought i could get the number of form objects within the form using "formName.length." Then use a loop to go through all the form fields.

It seems formName.length is not working for me ... it's returning "undefined" or "0" values. How could i get the number of form objects within a certain form.

My code is as follows:

<script language="JavaScript">
window.moveTo(100,100);
window.resizeTo(300,460);
//window.locationbar.visible=false;
//window.menubar.visible=false;
//window.personalbar.visible=false;
//window.scrollbars.visible=false;
//window.statusbar.visible=false;
//window.toolbar.visible=false;
//window.resize=false;
//window.resizeable=no;

var elementNo;

elementNo = document.registrationForm.length;
//elementNo = document.registrationForm.txtFirstName.value;
var num;
num = document.forms[0].length;
var counter = 0;
var myMessage = "Please ensure that you have filled in the following fields:\n";
var myFieldsAlert = "";
var myAlertFlag="0";
function validate(){
//
alert("abc "+elementNo);
alert("abc "+num);
/**/
while(counter<elementNo){
//
if(registrationForm.elements[counter].value==""){
myFieldsAlert += "-"+registrationForm.elements[counter].id+"\n";
myAlertFlag="1";
}

if(registrationForm.optGenderM.value!="0" || registrationForm.optGenderF.value!="1"){
myFieldsAlert += "-Gender\n";
myAlertFlag="1";
}
counter++;
}
/**/
/**/
if(myAlertFlag=="1"){
alert(myMessage+myFieldsAlert);
}
else{
registrationForm.submit();
}

myAlertFlag ="0";
/**/
return false;
}
</script>
Could someone please enlighten me on the issue at hand and propose a solution. (By the way the code is in a HTML page which is called up as a popup).

Any help would be greatly appreciated...

szpilman
October 5th, 2006, 05:32 AM
hi.. i have tried the same code, and it works well
maybe
the problem is because u call the length of the form before creating the form.
i mean, i am calling the script that validate the field values after i create the form.

this is my code ...


<html>

<body>

<form method="POST">

<input type="text" name="T1" size="20">
<input type="submit" value="Submit" name="B1" onclick='validate()'>
<input type="reset" value="Reset" name="B2"></p>
</form>

</body>

<script language='javascript'>
function validate(){
t = document.forms[0].length
alert(t);

}
</script>
</html>

PeejAvery
October 5th, 2006, 07:12 AM
the problem is because u call the length of the form before creating the form.
That would be my assumption also, but you cannot assume that. Remember that JavaScript can be called at any time. Since this is not a function, this JavaScript might be placed after the form creation.

@braveheartkenya, if this did not solve your problem, can you please post your code with the form, and not just the Javascript referencing the page?

szpilman
October 6th, 2006, 11:29 AM
yeah ... just my assumption..
actually there maybe effects from other line of ur codes ..
please be kindly post it here :)

PeejAvery
October 6th, 2006, 12:32 PM
yeah ... just my assumption..
actually there maybe effects from other line of ur codes ..
please be kindly post it here :)
Pretty much identical to my post.