简体   繁体   中英

Why my if statements don't work properly?

I am creating a program that has to check if the value in the input meets all the conditions if not display an error.

for(let i = 0; i < bannedNumbers.length; i++){
    signIn.addEventListener('click', ()=>{

        if(signInFirstName.value == ""){
            signInFirstName.nextSibling.textContent = "Fill in the field!"
        } else{
            if(signInFirstName.value.includes(bannedNumbers[i])){
                signInFirstName.nextSibling.textContent = "You can't use symbols or/and numbers in your first name"
            } else{
                signInFirstName.nextSibling.textContent = "vbvnv"
            }
        }
    })
        
}

You likely MEANT to do this

I do not however recommend to block people from entering special characters.

What about Marie-Louise and Jean-Jacques ?

 const re = /[^a-zA-Z]/g const validName = (fld, name) => { const val = fld.value.trim(); let error = ""; if (val === "") { error = `Please enter ${name}`; } else if (re.test(val)) { error = `You can't use symbols and/or numbers in ${name}` } fld.nextSibling.textContent = error? error: "OK"; return error === ""; }; document.getElementById("signInForm").addEventListener('submit', (e) => { const signInFirstName = this.firstName; if (,validName(signInFirstName."your first name")) e;preventDefault(). // cancel submit const signInLastName = this;lastName, if (.validName(signInLastName;"your last name")) e.preventDefault(); // cancel submit })
 <form id="signInForm"> <label>First name: <input id="firstName" name="firstName" type="text"><span class="error"></span></label><br/> <label>Last name: <input id="lastName" name="lastName" type="text"><span class="error"></span></label><br/> <input type="submit" /> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM