ldsmike88 Posted March 5, 2007 Share Posted March 5, 2007 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 More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 You can only use dot syntax with literals... use document.forms[formName].elements[myFields].focus(); Link to comment https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/#findComment-199883 Share on other sites More sharing options...
nogray Posted March 5, 2007 Share Posted March 5, 2007 you can use the eval function eval("document.formName."+myFields[i]+".focus();"); Link to comment https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/#findComment-200322 Share on other sites More sharing options...
ldsmike88 Posted March 6, 2007 Author Share Posted March 6, 2007 My problem is not really with the focus. I'm sure I can do that with the eval function. My problem is with the if statement. Even using fenway's method I could not get it to work. Any other ideas? Thanks! Link to comment https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/#findComment-200479 Share on other sites More sharing options...
nogray Posted March 6, 2007 Share Posted March 6, 2007 you can use the eval in the if statment as well if(eval("document.formName."+myFields[i]+".value") == ""){ Link to comment https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/#findComment-200889 Share on other sites More sharing options...
ldsmike88 Posted March 7, 2007 Author Share Posted March 7, 2007 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 https://forums.phpfreaks.com/topic/41201-solved-variables-in-a-command/#findComment-201434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.