Jump to content

Form Validation


jaymc

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
Link to comment
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
Link to comment
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.
Link to comment
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}"
Link to comment
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]
Link to comment
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 !
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.