samedi 18 juillet 2020

Why does if conditional statement terminate after else if(which is false)although there is another else if in this if conditional statement?

I have written this code in nodejs:

let letters = /^[A-Za-z]+$/;
let letter2 = /^[a-z_]+$/;


const registerChecker = async(firstName,lastName,username, email, password, password2) =>{
    let errors = [] 
    // Check required fields
    if(!firstName||!lastName||!username|| !email|| !password || !password2){
        errors.push({msg:'Please fill all fields'})
    }else if(!firstName.match(letters)){
        errors.push({msg:'Firstname must contain only Latin lowercase and uppercase letters("a"-"z")("A"-"Z")'})
    }else if(!lastName.match(letters)){
        errors.push({msg:'Lastname must contain only Latin lowercase and uppercase letters("a"-"z")("A"-"Z")'})
    }else if(username){
        if(!username.match(letter2)){
            errors.push({msg:"Username must contain only lowercase Latin letters ('a' - 'z'), numbers, and underscores ('_')"})
        }
    }else if(password.length<6){
        console.log('Less than 6')
        // Password length
        errors.push({msg:'Password must be at least 7 characters'})
    }else if(password2 !== password){
        // Matching Password
        errors.push({msg:'Password do not match'})
    }
    
    
    return errors
}
registerChecker('firstname','lastname','user_me','hello@gmail.com','pass','fs').then(s=>console.log(s)).catch(er=>console.log(er))

When I run this code, it is working till (else if(password.length<6)) perfectly. But this (else if(password.length<6)) else if and under it does not work at all. What should I do in order to write this code properly?

Aucun commentaire:

Enregistrer un commentaire