I am trying to save my self from writing alot of code. is there anyway that i can put a variable in a document.formname.field.value statement?

var fieldname = "name_of_field_in_form";
var fname = document.register.fieldname.value;
if (fname == "")
{
alert("Please enter your first name.");
return false;
}
why doesnt this work^