Search code examples
javascripthtmlinclude

.includes method is always true in js


I have this little code to make a login with a key that needs to includes variuous strings... but the .includes method is always true can someone explain me what's wrong?

function validate() {
var keyInput = document.getElementById("keyInputBox").value;
console.log('User trying to log with key: "' + keyInput + '"');
var keyRequireBoolean0 = keyInput.includes('anon')
if (keyRequireBoolean0 = true) {
    var keyRequireBoolean1 = keyInput.includes('sup')
    if (keyRequireBoolean1 = true) {
        alert('login successfull ' + keyRequireBoolean0)
    } else {
        alert('Invaild Key');
    }
} else {
    alert('Invaild Key');
}}

and this is the html part:

  <input type="password" id="keyInputBox" placeholder="Key">
  <input type="submit" onclick="validate()" value="Check">

Solution

  • What you want to do is compare the boolean:

    function validate() {
    var keyInput = document.getElementById("keyInputBox").value;
    console.log('User trying to log with key: "' + keyInput + '"');
    var keyRequireBoolean0 = keyInput.includes('anon')
    if (keyRequireBoolean0 === true) {
        var keyRequireBoolean1 = keyInput.includes('sup')
        if (keyRequireBoolean1 === true) {
            alert('login successfull ' + keyRequireBoolean0)
        } else {
            alert('Invaild Key');
        }
    } else {
        alert('Invaild Key');
    }}