PDA

View Full Version : Javascript: using for loop to validate form


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!

degsy
March 16th, 2006, 10:28 AM
You can use somethink like this


<script>
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


function validate(frm){
msg = '';
for(x=0; x<frm.elements.length; x++){
if(Trim(frm.elements[x].value) == ""){
msg += "Please enter your " + frm.elements[x].name + "\n";
}
}
if(msg){
alert(msg)
return false;
}
}
</script>

The Trim function is just from a google search for a javascript trim function.