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 ])+$/; 
  alert(field);
  //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;
	}
	else
	{
	 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?

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

alert(eval("obj."+field+".value"))

if field has "Extension", then it work as

obj.Extention.value

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.