0

I'm a newbie scripter and these are my first javascript attempts. What this code is supposed to do is: It's a calculator but first it requires registration. (It's kinda fiction though, as you can just press Continue without registering). What I wanted to do is: if the password I entered is less than 5 characters then it should say a message that an error occured and nothing to happen after that. If the password is more tahn 5 characters then it should continue on with the calculator.

So here is the code but no matter how many characters my password has it simply launches the calculator directly going to the else statement.

var pw = document.getElementByID("userInput2").value;

function triggerCalc(){
        if (pw<5){
                alert("An error occured. Your password must be more than 5 symbols!");
                }
        else { ... }
}
2
  • Include the code in this question. Commented Jan 3, 2014 at 12:22
  • Please post your code here, not on some 3rd party site. Commented Jan 3, 2014 at 12:22

4 Answers 4

1

Try:

if(pw.length < 5) {
 ...
}

The length part checks the length of the password. Beforehand, you were checking if the password itself is less than 5 (and it isn't a number, so that wouldn't make sense).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your fast response, but now nothing happens after I press Continue. Do you have an idea why this happens?
Nope, but that's a separate problem so raise a new question :-)
1

This will be a better fit

if(pw.length <= 5)

{
 alert("An error occurred. Your password must be more than 5 symbols!");
}
else{}

Comments

0

Just pw would return an object. You need to use the length property so that you could target it's length.

Try this:

if(pw.length <= 5) { //do something here }

Comments

0

I think you can do this:

function triggerCalc() {
    var pw = document.getElementByID("userInput2").value;
    if (pw.length < 5){
        alert("An error occured. Your password must be more than 5 symbols!");
    }
}

The variable pw should be inside the function, and not before it.

And a hint: some indentation in the code would be very good!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.