Have the Javascript function below which takes the name of an input on the form passed as a variable so that 1 function can check many inputs as I have up to 3 fields per form that can accept this data range.

function contact_number_check(formObj,field)
  var obj = document.forms[formObj];
  var regExp = /^([0-9 ])+$/; 
  //var check = obj.Extension.value;
  var check = obj.field.value;

	if (regExp.test(check) == false)
		alert("Invalid Contact Number entered only 0-9 and Spaces allowed in input field");
		return false;
	 return true;

it is called like this

var ext_check = contact_number_check(formObj,'Extension');

the problem is when I use the top line below it works if I name the field as it is called on the form, the field input box is named Extension in this instance. It does not work when I use the 2nd way of doing which is setting the value in the function parameter field and then passing it as a variable like I have in the 2nd line below

var check = obj.Extension.value;
var check = obj.field.value;

I know that field contains the value of Extension as when I alert(field); the out is the word: Extension.

Can I not use a variable this way in JS?

7 Years
Discussion Span
Last Post by asmira

i think you`ll need eval method..
check this code


if field has "Extension", then it work as



Edited by asmira: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.