skip to Main Content

I wanted a password with js that accepts both lowercase and uppercase letters. I’m also not sure if this code is the most correct for a password.

The password would be the insertion of certain words and phrases (e.g.: human, human being) that would direct to page A, any other text would direct to page B.

 <form>
                    <div class="puzzle">
                        <label for="password">
                            <i>Which animal has four legs in the morning, two in the afternoon, and three legs at night?</i>
                        </label>
                        <input
                            type="text"
                            id="password"
                            autocomplete="off"
                            placeholder="Your answer"
                            required="required">
                    </div>
                    <input type="button" class="btn" onclick="checkPassword()" value="Answer">

                </form>
function checkPassword() {
  var password = $('#password').val();
  if (password == 'human being') {
    window.location.href = '../minotaur/' }
  else {
    window.location.href = '../sphinxDead/'; 
  } 
}

2

Answers


  1. You can use the toLowerCase() (or toUpperCase()) functions to convert your string to your desired case, then compare that to your string. So you could do

    var password = $('#password').val().toLowerCase();
    if (password == 'human being') {
      window.location.href = '../minotaur/'
    } else {
      window.location.href = '../sphinxDead/'; 
    } 
    

    Which will now accept ‘human being’, ‘HUMAN BEING’, ‘huMAn BeINg’, etc.

    Login or Signup to reply.
  2. A word of caution regarding your approach. Your approach is a little vulnerable. So if anyone looked at the source code of the page, they would be able to work out what the answer is. Ideally the check should be done as part of a server sided operation.

    That said, the simplest thing i can think of for your example is to convert the value of the input to upper case. So for example the user can enter ‘human being’ ‘Human Being’ ‘HUMAN Being’, it should all match.

    function checkPassword() { 
        var password = $('#password').val(); 
        if (password.toUpperCase() == 'human being'.toUpperCase()) { 
            window.location.href = '../minotaur/' 
        } else {
            window.location.href = '../sphinxDead/'; 
        }
    }
    

    You could also do something like this and only call toUpperCase once:

    function checkPassword() { 
        var password = $('#password').val(); 
        if (password.toUpperCase() == 'HUMAN BEING') { 
            window.location.href = '../minotaur/' 
        } else {
            window.location.href = '../sphinxDead/'; 
        }
    }
    

    Hope that helps.
    Kind regards

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search