Jump to content

function problem


acidglitter

Recommended Posts

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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() ;'

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.