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
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);	
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.