jaymc Posted October 8, 2006 Share Posted October 8, 2006 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 aswellHowever, 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 nameHowever, I get a javascript error saying valid is undefinedI'm, just wondering why Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/ Share on other sites More sharing options...
yonta Posted October 8, 2006 Share Posted October 8, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-105863 Share on other sites More sharing options...
jaymc Posted October 8, 2006 Author Share Posted October 8, 2006 Ah I see. Well, if I put var valid=falseAt 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 Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-105890 Share on other sites More sharing options...
yonta Posted October 8, 2006 Share Posted October 8, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-105948 Share on other sites More sharing options...
jaymc Posted October 9, 2006 Author Share Posted October 9, 2006 Yeh thats what I thought. However, it still isnt workingIs it ok to to call the valid var up like this later down in the body of the document. If so its definetly not workingOnClick="if(valid==true){do what ever}" Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106243 Share on other sites More sharing options...
yonta Posted October 9, 2006 Share Posted October 9, 2006 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 sectionvar 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 <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] Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106547 Share on other sites More sharing options...
jaymc Posted October 10, 2006 Author Share Posted October 10, 2006 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106723 Share on other sites More sharing options...
fenway Posted October 10, 2006 Share Posted October 10, 2006 Are you sure this script block is above the onlick handler? Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106912 Share on other sites More sharing options...
jaymc Posted October 10, 2006 Author Share Posted October 10, 2006 100% sureits in the head Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106935 Share on other sites More sharing options...
fenway Posted October 10, 2006 Share Posted October 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/23355-form-validation/#findComment-106988 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.