www.webdeveloper.com
Results 1 to 9 of 9

Thread: Global variable not working

  1. #1
    Join Date
    Apr 2013
    Posts
    24

    Global variable not working

    I can't believe that I'm posting this as a problem. It's so basic, but I can't get it to work. I have a variable: passed. I want to use it in two different functions: checkit and validate. Here's the code:
    Code:
    passed = true;
    
    function checkit(what){
    :
    :
    :
    :
    if (!checked){
    passed = false;
    return false;
    }
    }
    
    function validate(what){
    if (!passed){
    :
    :
    :
    :
    }
    }
    I've read all the posts and the google search articles and nothing I try works. The value of 'passed' isn't passed to the validate function even when I force it to be false (set it to false as the first line in the checkit function).

    Can anyone take pity on a newbie and tell me what I'm doing wrong?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    Code:
    <!DOCTYPE>
    <html>
    <head>
    <title>Passed</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    passed=false;
    
    function doc(ID){return document.getElementById(ID);}
    function showPassed(){doc('var_passed').innerHTML=passed;}
    
    function checkit(what){
    passed=what.checked;
    showPassed();
    validate(what);
    }
    
    function validate(what){
    if(passed){doc('val_res').innerHTML='if(passed) has just worked';}
    if(!passed){doc('val_res').innerHTML='if(!passed) has just worked';}
    }
    
    
    window.onload=function(){
    doc('passed_val').onclick=function(){checkit(this);}
    showPassed();
    }
    </script>
    </head>
    <body>
    <center>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <input type="checkbox" id="passed_val" value="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>passed = <i id="var_passed" style="color:Crimson;"></i></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>validate(what) result = <i id="val_res" style="color:Salmon;">no result yet</i></b>
    </center>
    </body>
    </html>

  3. #3
    Join Date
    Apr 2013
    Posts
    24
    Padonak.

    Thank you for the quick reply. Unfortunately, I can't use the onload event handler as I'm already using it for something else. Also, I should have given more information. I'm calling checkit with the onblur event handler in my form <input> tags. I'm calling validate with the onsubmit event handler of the <form> tag.

    One more thing I should say. I'm doing this for a school project so if it gets too tricky I'll probably get nailed.

    I thought that all you had to do to have a variable available to multiple functions was to define it outside the functions. Am I wrong?

    Thanks anyway.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    i suppose that your checks are performed wrong way. i think we were already done with your page if you had posted the whole code in the very beginning of this thread

    as for global/local/scope etc. see this article or any other

  5. #5
    Join Date
    Apr 2013
    Posts
    24
    Hey man, I didn't mean to tick you off. I thought this was a simple problem that would have a simple answer. Here's the code:
    Code:
    var passed = true;
    function checkit(field) {
    var pattern="";
    var message="";
    passed = true ;
    if (field.value != "") {
        if(field.name == "First Name" || field.name == "Last Name") {
            pattern = /[^a-zA-Z\s\.'-]/g ;
            message = "Please enter only letters, spaces, appostophes, and dashes" ;
        }
        else if(field.name == "Address") {
            pattern = /[^a-zA-Z0-9\s\.-]/g ;
            message = "Please enter only letters, numbers, spaces, periods, and dashes" ;
        }
        else if(field.name == "City") {
            pattern = /[^a-zA-Z\s]/g ;
            message = "Please enter only letters & spaces" ;
        }
        else if(field.name == "State") {
            pattern = /[^a-zA-Z\s]/g ;
            message = "Please enter only letters & spaces" ;
        }
        else if(field.name == "Zipcode") {
            pattern = /[\D]/g ;
            message = "Please enter only numbers" ;
        }
        else if(field.name == "Phonenumber") {
            pattern = /[^0-9\s\.-]/g ;
            message = "Please enter only numbers." ;
        }
        else if(field.name == "Email") {
            pattern = /^[A-z0-9_\.]+@[A-z0-9_\.]+[.]([A-z]{2,3})$/i ;
            message = "You have entered an invalid email address" ;
            if(pattern.test(field.value)) return true;
            alert(message);
            field.value = "" ;
            field.focus() ;
            passed = false ;
            return false ;
        }
    var a = field.value;
    if(!pattern.test(a)) return true ;
    alert(message) ;
    field.value = "" ;
    field.focus() ;
    passed = false ;
    return false ;
    }
    }
    The function is called from a number of <input>'s, all are like this:
    Code:
    <font face="Arial" size=4>First Name:</font></td><td align="left"><input name="First Name" id="Required" size=50 onblur="checkit(this)">
    The validate function is called by:
    Code:
    <form id="mailList" name="mailList" action="Script_Files/Process_Form.php" method="post" onsubmit="return validate(this);">
    The only thing I have in the validate function is:
    Code:
    function validate(which) {
    alert(passed) ;
    {
    The alert always returns true.

    I looked at the articles and it looks like this should work. I could really use your help.

    BK

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    sorry for the delay it was a hard day today. try this code (some of your regexp's changed, but i think you have to make them much strictly)

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>ttl</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <style type="text/css">input{text-align:center;}</style>
    </head>
    <body>
    <form id="mailList" name="mailList" action="Script_Files/Process_Form.php" method="post">
    <table align="center" border="0" cellspacing="3" cellpadding="5"><tbody>
    <tr><td><font face="Arial" size="4">First Name:</font></td><td align="left"><input name="firstname" id="Required1" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">Last Name:</font></td><td align="left"><input name="lastname" id="Required2" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">Address:</font></td><td align="left"><input name="address" id="Required3" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">City:</font></td><td align="left"><input name="city" id="Required4" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">State:</font></td><td align="left"><input name="state" id="Required5" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">Zipcode:</font></td><td align="left"><input name="zipcode" id="Required6" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">Phonenumber:</font></td><td align="left"><input name="phonenumber" id="Required7" size="50" /></td></tr>
    <tr><td><font face="Arial" size="4">Email:</font></td><td align="left"><input name="email" id="Required7" size="50" /></td></tr>
    <tr><td colspan="2" align="center"><input type="submit" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" /></td></tr>
    </tbody></table>
    </form>
    <script type="text/javascript">
    var path=[
    ["firstname","[a-zA-Z\s\.'-]{1,}","g","Please enter only letters, spaces, appostophes, and dashes"],
    ["lastname","[a-zA-Z\s\.'-]{1,}","g","Please enter only letters, spaces, appostophes, and dashes"],
    ["address","[a-zA-Z0-9\s\.-]{1,}","g","Please enter only letters, numbers, spaces, periods, and dashes"],
    ["city","[a-zA-Z\s]{1,}","g","Please enter only letters & spaces"],
    ["state","[a-zA-Z\s]{1,}","g","Please enter only letters & spaces"],
    ["zipcode","[\d]{10,}","g","Please enter only 10 numbers"],
    ["phonenumber","[\d]{10,}","g","Please enter only 10 numbers"],
    ["email","^[A-z0-9_\.]+@[A-z0-9_\.]+[.]([A-z]{2,3})$","i","You have entered an invalid email address"]
    ],
    frm=document.getElementById('mailList'),fields=frm.elements,passed=true;
    
    function chk(obj){
    for(var k in path){
    if(path[k][0]!==obj.name){continue;}
    else{
    var mask=new RegExp(path[k][1],path[k][2]);
    if(!mask.test(obj.value)){
    if(chk.arguments[1]=='alrt_on'){alert(path[k][3]);}
    obj.value="";obj.focus();passed=false;break;
    }
    }
    }
    }
    
    for(var i in fields){
    if(!fields[i].name && !fields[i].id){continue;}
    else{fields[i].onchange=function(){chk(this,'alrt_on');}}
    }
    
    frm.onsubmit=function(){
    for(var n in fields){
    if(!fields[n].name && !fields[n].id){continue;}
    else{chk(fields[n]);}
    }
    if(!passed){alert('Some fields may have incorrect format! Please, re-check all the information you entered and try again (OR BURN IN HELL YOU STUPID ASS).');return false;}
    else{return true;}
    }
    </script>
    </body>
    </html>

  7. #7
    Join Date
    Apr 2013
    Posts
    24
    Thanks Padonak. This is great! I'm going to use it pretty much as you wrote it, if you don't mind. Of course, I'll have to change the alert message a bit.

    One thing, though. Works great in Foxfire but doesn't seem to work in IE. Don't know if it's the version I'm running (IE8) or not. Any thoughts?

    Thanks again.
    B.K.

  8. #8
    Join Date
    Apr 2013
    Posts
    24
    Padonak. Disregard. I made a couple of changes in my code and it works fine. Again, many thanks.

  9. #9
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    good to hear that was useful ) have fun, buddy )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles