Get Field Value
This function grabs the value from a passed in field. You should pass in the handle to the field, as in "document.forms[0].MyField". The return value is either a string or an array, depending on the field type (a check box would return an array).function getFieldValue(field)
{
switch(field.type)
{
case "text" :
case "textarea" :
case "password" :
case "hidden" :
return field.value;
case "select-one" :
var i = field.selectedIndex;
if (i == -1) return "";
else return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
case "select-multiple" :
var allChecked = new Array();
for(i = 0; i < field.options.length; i++)
if(field.options[i].selected)
allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
return allChecked;
case "button" :
case "reset" :
case "submit" :
return "";
case "radio" :
case "checkbox" :
if (field.checked) { return field.value; } else { return ""; }
default :
if(field[0].type == "radio")
{
for (i = 0; i < field.length; i++)
if (field[i].checked)
return field[i].value;
return "";
}
else if(field[0].type == "checkbox")
{
var allChecked = new Array();
for(i = 0; i < field.length; i++)
if(field[i].checked)
allChecked[allChecked.length] = field[i].value;
return allChecked;
}
else
var str = "";
for (x in field) { str += x + "\n"; }
alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
break;
}
return "";
}
Any field can be processed by this function. This function is used in the Generic Form Validation JavaScript.