简体   繁体   中英

I need to check lenght of password but for some reason .lenght doesnt work?

  • If i do password1.lenghth in if() and test the lenght it still displays that passwords are shorter than 5.
  • I'm still in school so we are doing simple stuff that might seem obvious but are not to me.
  • Also tried putting var pass_lenght= password1.lenght; and var pass_lenght= document.getElementById("password1").lenght; didn't work Anyone knows why?

Thanks

    var password1= document.getElementById("password1");
    var password2= document.getElementById("password2");
    password1.addEventListener("blur",function()
    {

        if (password1!= password2) {     
            password1.setAttribute("class","wrong");
            password2.setAttribute("class","wrong");
            return false;
            } 

            else if(password1.lenght<5)
            {
            password1.setAttribute("class","wrong");
            return false;

            }
            else
            {
            password1.setAttribute("class","correct");
            }
    });




    password1.addEventListener("blur",function()
    {

        if (password1!= lozinka2) {     
            password1.setAttribute("class","wrong");
            password2.setAttribute("class","wrong");
            return false;
            } 

            else if(password2.lenght<5)
            {
            password2.setAttribute("class","wrong");
            return false;
            }
            else
                {
            password2.setAttribute("class","correct");
        }
    });

What seems to be the problem is that you are not actually accessing the length. Your getElementById does exactly what it says, get an element by id. But you want the value entered, not the element.

You: password1.length
Correct: password1.value.length

Also, even if you change that, the else if statement wouldn't even be reached, so that would be pointless.

So, only change the password1 and password2 variables to:

var password1= document.getElementById("password1").value;
var password2= document.getElementById("password2").value;

There are some flaws in the above code which are as follows:

  1. If you want the actual text you should use the value attribute.
var password1= document.getElementById("password1").value;
  1. It might be because your spelling of length is wrong.

You normally measure the length of an input field but you can never measure the length directly off an element. You will have to get the value of input field by:

var password1= document.getElementById("password1").value;

console.log(password1.length) //======> this will print the length.

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