Jump to content


Photo

Form Validation


  • Please log in to reply
9 replies to this topic

#1 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 08 October 2006 - 02:22 PM

Ok, I have this bit if javascript for form validation which works great

function validate_form ( )
{
    valid = true;

    if ( document.form.artistnametxt.value == "If not, write it here" || document.form.artistnametxt.value == "")
    {
        alert ( "You must select an artist name!!\n\nEither do so from the drop down list or input it manually" );
        valid = false;
    }


AS you can see, its setting a variable called valid, that works great aswell

However, further down in the HTML I have this in a submit button

OnClick="if(valid==true){this.className='uploadgo'}"


In other words, if after form validation valid = true then it will change the class name

However, I get a javascript error saying valid is undefined

I'm, just wondering why
I would love to change the world, but they won't give me the source code

SEO Agency

#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 08 October 2006 - 02:43 PM

It has to do with variable scope. You defined the valid variable inside the function so it is only available inside that same function. To make it work you need to make the valid variable global. Just do this in the beginning of your script:
var valid= false;

Check this article http://www.webdevelo...javascript.php3
do it, do it right, do it right now

#3 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 08 October 2006 - 03:41 PM

Ah I see. Well, if I put

var valid=false

At the begining of the script, how can it ever change to true if its not part of the function which of course allows valid to be dynamic
I would love to change the world, but they won't give me the source code

SEO Agency

#4 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 08 October 2006 - 06:13 PM

Well, if you put it outside of any function, you effectively make it global, and so it is available inside your functions. So when you do valid=true inside a function you are effectively changing the global variable valid from false to true.
do it, do it right, do it right now

#5 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 09 October 2006 - 02:09 PM

Yeh thats what I thought. However, it still isnt working

Is it ok to to call the valid var up like this later down in the body of the document. If so its definetly not working

OnClick="if(valid==true){do what ever}"
I would love to change the world, but they won't give me the source code

SEO Agency

#6 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 09 October 2006 - 11:02 PM

It should be okay.

Here's a snippet - just check your code against this or if the error persists post your code here
//javascript in head section
var valid;

function validate_form ( )
{
   
    if ( document.form.artistnametxt.value == "If not, write it here" || document.form.artistnametxt.value == "")
    {
        alert ( "You must select an artist name!!\n\nEither do so from the drop down list or input it manually" );
        valid = false;
    } else {
		 alert ( "Good boy");//just for testing
		 valid = true;
	}
}

//html in body section, notice i have 2 css styles named blue and red
<form id="form" name="form" method="post" action="">
Artist &nbsp;<input type="text" name="artistnametxt" id="artistnametxt" value="If not, write it here"/><br/><br/>

    <input type="button" name="Submit" class="red" value="Validate" onClick="validate_form();"/><br/>
<input type="button" name="Submit" class="red" value="Check" onClick="if(valid==true){this.className='green'} else {this.className='red' };"/>
</form>

do it, do it right, do it right now

#7 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 10 October 2006 - 10:37 AM

Nope! Still doesnt work

<script language="JavaScript">

var valid;
function validate_form ( )
{

    if ( document.form.artistnametxt.value == "If not, write it here" || document.form.artistnametxt.value == "")
    {
        alert ( "You must select an artist name!!\n\nEither do so from the drop down list or input it manually" );
        valid = false;
    }

else {
		valid = true;
	}

    return valid;
}

//-->

</script>

I cant see how the hell I could be going wrong !

I would love to change the world, but they won't give me the source code

SEO Agency

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 03:45 PM

Are you sure this script block is above the onlick handler?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#9 jaymc

jaymc
  • Members
  • PipPipPip
  • Advanced Member
  • 1,521 posts
  • LocationLiverpool

Posted 10 October 2006 - 04:00 PM

100% sure

its in the head
I would love to change the world, but they won't give me the source code

SEO Agency

#10 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 05:02 PM

Strange... why don't you move all the logic from the onclick handler to a function, and take it from there.  It's cleaner anyway.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users