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
You can use the
toLowerCase()
(ortoUpperCase()
) functions to convert your string to your desired case, then compare that to your string. So you could doWhich will now accept ‘human being’, ‘HUMAN BEING’, ‘huMAn BeINg’, etc.
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.
You could also do something like this and only call toUpperCase once:
Hope that helps.
Kind regards