Redirect to pages based on input form field value (JS tips)


A quick JavaScript / jQuery snippet for your soul. Validate input form field value based on specific correct text value, if correct redirect to correct page; and if wrong, to error page.

function checkAnswer(){
      var response = document.getElementById('answer').value; // form elem
      if (response == "correctanswer") // check for this value
          location = 'http://www.correcturl.com'; 
      
      else
          location = 'http://www.wrongurl.com'; // incorrect if not found
      return false;
  }

If you want to use the hash URL parameter for your ‘correct’ and ‘incorrect’ pages – check out my previous JS post for use of hash.


Published by

admin

I’m Cameron Cashwell, I’ve been a Web Developer for 6+ years. I’m always on the look out for exciting new projects and ideas. Wanna work together? http://www.cdev.co/#request

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz