title: JavaScript global variable not visible inside function
body: <p>Im trying to use global variables for my js and its like below code</p>
  
  <pre><code>var fullName = document.getElementById("elementId").value;
  var nameFormat = /^[A-Za-zÀ-ú]+ [A-Za-zÀ-ú]+$/;
  </code></pre>
  
  <p>but when I try to use it inside a function function dosen't work .So I just pasted the var fullName into the function and then it works. So please tell me can I assign values into a global variable via <strong>document.getElementById("elementId").value;</strong>
  When I try to use nameFormat inside function it works either declare inside or outside.
  This is the full code</p>
  
  <pre><code>//common js regular expressions
  var emailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  var nameFormat = /^[A-Za-zÀ-ú]+ [A-Za-zÀ-ú]+$/;
  
  //global variables
  var fullName = document.getElementById("jsname").value;
  var email = document.getElementById("jsun").value;
  
  function validateForm() {
  
      //var fullName = document.getElementById("jsname").value;
  
      //onsubmit full name validation
  
      if (!(fullName.match(nameFormat))) {
          document.getElementById("pidnamevalidate").innerHTML = "Name should be in a valid format";
          document.getElementById("pidnamevalidate").style.color = 'green';
          return false;
  
          //onsubmit email validation
  
      } else {
          return true;
      }
  }
  </code></pre>