topical media & game development

talk show tell print

basic-ajax-09-FormValidationOld.js / js



  var xHRObject = false;
  
  if (window.ActiveXObject)
  {
  xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest)
  {
  xHRObject = new XMLHttpRequest();
  }
  
  function getData()
  {
      if ((xHRObject.readyState == 4))
      {
      if (xHRObject.status == 200)
      {
           var serverText = xHRObject.responseText;
           if (serverText == "True")
           {
              span.innerHTML = "This user name has already been taken";
           }
           if (serverText == "False")
           {
              span.innerHTML = "This user name is available";
           }
      }
         else
     {
          span.innerHTML = "We cannot currently assess whether this user name is 
  taken or not. It will be checked when you submit the form";
     }
  
      }
  }
  
  function getBody(newform, data)
  {
       var argument = data + "=";
       argument += encodeURIComponent(document.getElementById(data).value)  
       return argument;
  }
  
  function Validate(data)
  {
      var newform = document.forms[0];
      var bodyofform = getBody(newform, data); 
      if (bodyofform != "UserName=")
      {
      xHRObject.open("POST", <validate.php>, true);
      xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xHRObject.onreadystatechange = getData;
      xHRObject.send(bodyofform); ;  
      }
      else
      {
          span.innerHTML = "Blank user names not allowed";
      }
       
  }
  
  function Change()
  {
      if(state.innerHTML == "State:")
      {
       state.innerHTML = "County:"
       zipcode.innerHTML = "Postcode:"
      }
      else
      {
       state.innerHTML = "State:"
       zipcode.innerHTML = "Zipcode:"
      }
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.