Jump to content

Archived

This topic is now archived and is closed to further replies.

jaymc

Form Validation

Recommended Posts

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

[quote]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;
    }[/quote]

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

[quote]OnClick="if(valid==true){this.className='uploadgo'}"[/quote]

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

Share this post


Link to post
Share on other sites
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.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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}"

Share this post


Link to post
Share on other sites
It should be okay.

Here's a snippet - just check your code against this or if the error persists post your code here
[code]
//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>
[/code]

Share this post


Link to post
Share on other sites
Nope! Still doesnt work

[code]<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>[/code]

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

Share this post


Link to post
Share on other sites
Are you sure this script block is above the onlick handler?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.