...

View Full Version : Variables and HTML Forms



J20gU3
09-01-2011, 09:31 PM
Currently I have the following script:-



function validate_form(){
if(regform.reguser.value == ""){
alert("Please completed the selected box");
regform.reguser.focus();
return false;
}else if(regform.regemail.value == ""){
alert("Please completed the selected box");
regform.regemail.focus();
return false;
}else if(regform.regpass1.value == ""){
alert("Please completed the selected box");
regform.regpass1.focus();
return false;
}else if(regform.regpass2.value == ""){
alert("Please completed the selected box");
regform.regpass2.focus();
return false;
}else{
return true;
}
}


to validate a simple registration form, however I initially tried to streamline this function by cycling through an array using a loop to point to various input elements in the HTML page itself. I found that when trying to use a variable in the aforementioned if statements the javascript failed to work i.e.



var test = "reguser";
if(regform.test.value = ""){
}


I know the javascript is looking for the input element "test" instead of "reguser" but is there any way I can force it to look for the contents of the variable.

glenngv
09-01-2011, 09:59 PM
regform.elements[test].value

Old Pedant
09-01-2011, 10:02 PM
var test = "reguser";
if(regform[test].value = ""){ ... }


EDIT: Oh, man! THREE minutes. I really am slowing down!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum