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
https://forums.phpfreaks.com/topic/23355-form-validation/
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
https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-105863
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
https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106547
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
https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106723
Share on other sites

Archived

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

×
×
  • 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.