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 Quote Link to comment 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(); Quote Link to comment 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();"); Quote Link to comment 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! Quote Link to comment 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") == ""){ Quote Link to comment 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); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.