acidglitter Posted February 11, 2008 Share Posted February 11, 2008 i need to make a javascript that will check a form for the class i'm taking. but i'm having problems with the functions.. this is what i have <script type="text/javascript" language="javascript"> f_name = document.form.f_name.value; l_name = document.form.l_name.value; address1 = document.form.address1.value; city = document.form.city.value; state = document.form.state.value; zip = document.form.zip.value; country = document.form.country.value; email = document.form.email.value; function checkForm(){ // first checks that required fields aren't empty if((f_name=="") || (l_name=="") || (address1=="") || (city=="") || (state=="") || (zip=="") || (country=="") || (email=="")){ alert("You didn't fill out something required."); } return false; } </script> the function checkForm() isn't getting the names i set before the function. i tried putting var in front of each name (like var email) but it still won't work unless the fields are named inside the function. how can i fix this? because i'm going to have a lot of different functions and i don't want to name the same form fields over and over. Quote Link to comment Share on other sites More sharing options...
nogray Posted February 11, 2008 Share Posted February 11, 2008 You can't access the document element until the page has loaded, If you put this before the </body> tag it should work. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 11, 2008 Share Posted February 11, 2008 the code to grab the values of the form fields has to be inside the function so that the current values of the fields are grabbed when the function is called <script type="text/javascript" language="javascript"> function checkForm(){ // grab the values f_name = document.form.f_name.value; l_name = document.form.l_name.value; address1 = document.form.address1.value; city = document.form.city.value; state = document.form.state.value; zip = document.form.zip.value; country = document.form.country.value; email = document.form.email.value; // first checks that required fields aren't empty if((f_name=="") || (l_name=="") || (address1=="") || (city=="") || (state=="") || (zip=="") || (country=="") || (email=="")){ alert("You didn't fill out something required."); } return false; } </script> I don't know where you are calling this code, but you may want to 'return true' if it does pass all tests (would cause the normal form submital to take place) Quote Link to comment Share on other sites More sharing options...
acidglitter Posted February 12, 2008 Author Share Posted February 12, 2008 yea it will return true when i'm finished making this... but i was wondering if there is any way to name the form fields BEFORE the function because i'm going to have a lot of functions all of this is happening before the </head> tag Quote Link to comment Share on other sites More sharing options...
nogray Posted February 12, 2008 Share Posted February 12, 2008 better option is to make a function to get the value, so you dont have to worry about variable scope or page load function getVale(obj){ return document.form[obj].value; } function checkForm(){ // first checks that required fields aren't empty if((getVale('f_name')=="") ............ } Not tested Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 12, 2008 Share Posted February 12, 2008 i see, you don't want to repeat the calls to the form fields in every function. then make the call to get the form fields a function of it's own: var myfields = new Array(); function getFormFields() { myfields['f_name'] = document.form.f_name.value; myfields['l_name'] = document.form.l_name.value; myfields['address1'] = document.form.address1.value; myfields['city'] = document.form.city.value; myfields['state'] = document.form.state.value; myfields['zip'] = document.form.zip.value; myfields['country'] = document.form.country.value; myfields['email'] = document.form.email.value; } --I made myfields a global array. Every time you call getFormFields() the global myfields array will be set with the latest form values. You can then just do this: function checkForm(){ // grab the values getFormFields() ; // first checks that required fields aren't empty if((myfields['f_name']=="") || (myfields['l_name']=="") || (myfields['address1']=="") || (myfields['city']=="") || (myfields['state']=="") || (myfields['zip']=="") || (myfields['country']=="") || (myfields['email']=="")){ alert("You didn't fill out something required."); } return false; } so every time you want to get the latest form field values just call 'getFormFields() ;' 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.