Jump to content

[SOLVED] Variables in a command


ldsmike88

Recommended Posts

For some reason I cannot include a variable in a command. If I do include a variable it gives me and error like "document.formName.myFields is null or not a variable." I cannot make this work. Here is my function:

 

function checkFields(formName, fields){
var myFields = fields.split('; ');
var trueORfalse = 0;
for(var i = 0; i < myFields.length; i++){ 

	if(document.formName.myFields[i].value == ""){
		alert('The information has not been submitted! You did enter information in the ' + myFields[i] + ' field.');
		document.formName.myFields[i].focus();
		trueORfalse++;
		if(trueORfalse != 0){
			return(false);
		}
	}
}
if(trueORfalse == 0){
	return(true);
} else {
	return(false);	
}
}

 

If anyone knows how to make this work please let me know! Thanks!

 

Michael

Link to comment
https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/
Share on other sites

Ok, I got it to work with the eval function. It took me a long time but here is the finished product for anyone who wants it! Thanks everyone who helped!

 

In the form: onsubmit="return checkFields(this.name, 'FirstName; LastName; Username; Password; SecondPassword');"

The FirstName; LastName... and so on, are the names of the required input text fields.

 

Here is my new updated script:

 

function checkFields(formName, fields){
var myFields = fields.split('; ');
var trueORfalse = 0;
for(var i = 0; i < myFields.length; i++){ 
	var thisField = myFields[i];
	if(eval("formName='"+formName+"'; thisField='"+thisField+"'; document."+formName+"."+thisField+".value;") == ""){
		alert('The information has not been submitted! You did enter information in the ' + thisField + ' field.');
		eval("formName='"+formName+"'; myFields='bogusInfo'; document."+formName+"."+thisField+".focus();");
		trueORfalse++;
		return(false);
	}
}
if(trueORfalse == 0){
	return(true);
} else {
	return(false);	
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.