Click to See Complete Forum and Search --> : form values


zyex
09-27-2003, 10:32 AM
Hi,

I'm trying to get the values from a form...

what is the correct method for getting the value of form fields when the form name and field names are passed in the parameters of the function


I have been trying this method and its not working..

function f(form, field)
{
var theFormName = document[form].name;
var val = document[theFormName].field.value;
}

and also this one...

function f(form, field)
{
var theFormName = document[form].name;
var val = document[theFormName[field]].value;
}

can anyone help?

Thanks,

Khalid Ali
09-27-2003, 10:39 AM
there are several possible options..show us the HTML code where you are passing the form reference...

zyex
09-27-2003, 10:49 AM
heres what i'm trying to do...

<html>
<head>
<title>Form Validation Test</title>

<script language="JavaScript">
<!--

function validateForm(form)
{
var form_name = document[form].name;
var req = document[form_name].required.value;
var req_fields = explodeArray(req,',');
var no_req_fields = req_fields.length -1;
var msgStr = "";

for(var i=0; i <= no_req_fields; i++)
{
var field = reqFields[i];
var val = document[form_name[field]].value;

if(val == "" )
{
msgStr += "- " + (reqFields[i] + "\n");
}
}

if(msgStr != "")
{
alert("You must complete the following form fields:\n\n" + msgStr);
return false;
}

else
{
alert("form ok");
return true;
}
}

function explodeArray(item,delimiter)
{
tempArray=new Array(1);
var Count=0;
var tempString=new String(item);
while(tempString.indexOf(delimiter)>0)
{
tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
Count=Count+1
}
tempArray[Count]=tempString;
return tempArray;
}
//-->
</script>
</head>

<body>
<form name="form_one" id="form_one" action="validator.htm" method="post">
<input type="Hidden" name="required" value="name,age" id="required">
<input type="text" name="name" id="name"><br>
<input type="text" name="age" id="age"><br>
<input type="submit" name="submit" id="submit" value="Submit" onClick=" return validateForm('form_one')">

</form>

</body>
</html>

Khalid Ali
09-27-2003, 11:20 AM
Here is the working validateForm function..make sure you notice the differences and then try to understand them

function validateForm(frm){
var req = frm.required.value;

var req_fields = explodeArray(req,',');
var no_req_fields = req_fields.length -1;
var msgStr = "";

for(var i=0; i <= no_req_fields; i++){
var field = req_fields[i];
var val = (eval('frm.'+field)).value;
if(val == "" ){
msgStr += "- " + (reqFields[i] + "\n");
}
}
if(msgStr != ""){
alert("You must complete the following form fields:\n\n" + msgStr);
return false;
}else{
alert("form ok");
return true;
}
}


and below is the updated HTML part of it

<form name="form_one" id="form_one" action="validator.htm" onsubmit="return validateForm(this)"method="post">
<input type="Hidden" name="required" value="name,age" id="required">
<input type="text" name="name" id="name"><br>
<input type="text" name="age" id="age"><br>
<input type="submit" name="submit" id="submit" value="Submit" >

zyex
09-27-2003, 11:28 AM
thanks for the help and prompt reply

Khalid Ali
09-27-2003, 11:30 AM
:)
you are welcome