batty13
March 16th, 2006, 01:47 AM
Hi,
Im hoping someone could help me figure out what I am mising here. Ive tried everything i can think of to get this to work, but it keeps coming up short. I've looked over my book and searched all over the web. Exhausting every resource Ive decided to ask for help.
I have a simple form that requests user information (actually its much longer than this, but just to keep things simple ive included three)
//////////////////////////////////////////////////////////////////////////////////
<form name="form1" onSubmit="return validate();">
<p>Name:
<input type="text" size="20" name="yourname" /></p>
<p>Address:
<input type="text" size="30" name="address" /></p>
<p>Phone:
<input type="text" size="30" name="phone" /></p>
<input type="submit" value="submit" />
</form>
//////////////////////////////////////////////////////////////////////////////////
Here is the javascript function. I also wanted to create an array for each inputs' value so that I can create a string/name association with each input.
Thus when the for loop finds a blank field in (array[i]) it i wll alert user
alert ("please input your" + array[i].name + "."); This part Im confident I can get working and it tests in a test alert set prior to for loop. The issue I have is i cant seem to get it to recognize [i] as the document.form1.yourname.value.length part in a for loop when i replace the "yourname" part with array[i].
//////////////////////////////////////////////////////////////////////////////////
formarray[0].name = "Your Name."
formarray[1].name = "Your Address."
formarray[2].name = "Your Phone."
formarray =["elemA","elemB","elemC"];
elemA=["yourname"]
elemB=["address"]
elemC=["phone"]
function validate() {
for (var i in formarray)
{
if ([i].value.length < 3)
{
alert("Please enter your" + formarray[i].name + ".");
return false;
}
}
return true;
}
Thanks in Advance!
Im hoping someone could help me figure out what I am mising here. Ive tried everything i can think of to get this to work, but it keeps coming up short. I've looked over my book and searched all over the web. Exhausting every resource Ive decided to ask for help.
I have a simple form that requests user information (actually its much longer than this, but just to keep things simple ive included three)
//////////////////////////////////////////////////////////////////////////////////
<form name="form1" onSubmit="return validate();">
<p>Name:
<input type="text" size="20" name="yourname" /></p>
<p>Address:
<input type="text" size="30" name="address" /></p>
<p>Phone:
<input type="text" size="30" name="phone" /></p>
<input type="submit" value="submit" />
</form>
//////////////////////////////////////////////////////////////////////////////////
Here is the javascript function. I also wanted to create an array for each inputs' value so that I can create a string/name association with each input.
Thus when the for loop finds a blank field in (array[i]) it i wll alert user
alert ("please input your" + array[i].name + "."); This part Im confident I can get working and it tests in a test alert set prior to for loop. The issue I have is i cant seem to get it to recognize [i] as the document.form1.yourname.value.length part in a for loop when i replace the "yourname" part with array[i].
//////////////////////////////////////////////////////////////////////////////////
formarray[0].name = "Your Name."
formarray[1].name = "Your Address."
formarray[2].name = "Your Phone."
formarray =["elemA","elemB","elemC"];
elemA=["yourname"]
elemB=["address"]
elemC=["phone"]
function validate() {
for (var i in formarray)
{
if ([i].value.length < 3)
{
alert("Please enter your" + formarray[i].name + ".");
return false;
}
}
return true;
}
Thanks in Advance!