0

I want to use this javascript but being able to hard code 3 users, so that those 3 are able to Login. I'm a newbie with javascript so yea. The below is the code i use for a single user login.

function validate(loginForm)
{
 var booValid = true;
 var strErrorMessage = "";
 var minLength=5;
 var maxLength=10;

 if(loginForm.pword.value.length < minLength)
 {
  strErrorMessage = "password must at least 5 characters\n";
  booValid=false;
 }

 if(loginForm.pword.value.length > maxLength)
 {
  strErrorMessage = "pasword must not more than 10 characters\n";
  booValid=false;
 }
 if(loginForm.loginid.value.indexOf("[email protected]") == -1)
 {
    strErrorMessage = "Incorrect Login ID, Please try again\n";
    booValid = false;
 }
 else if(loginForm.loginid.value.indexOf("[email protected]") == -1)
 {
    strErrorMessage = "Incorrect Login ID, Please try again\n";
    booValid = false;
 }
 else if(loginForm.loginid.value.indexOf("[email protected]") == -1)
 {
    strErrorMessage = "Incorrect Login ID, Please try again\n";
    booValid = false;
 }

 if(!booValid)
 {
  alert(strErrorMessage);
 }

 return booValid;
}

I've tried to use "else if" but still doesn't work. It says the users name is wrong.

I've also try as:

if(loginForm.pword.value.indexOf("[email protected]") == -1) || loginForm.pword.value.indexOf("[email protected]") == -1 || loginForm.pword.value.indexOf("[email protected]") == -1)
{
strErrorMessage = "Incorrect Login ID, Please try again\n";
booValid=false;
}

Please help!

3
  • 2
    Uhhhm.... JavaScript and login does not fit into one sentence. Even if you hardcode your 3 users with JS, every user can circumvent this by just turning Javascript of or overwriting your functions to be able to use another username. I hope you don't do the password checking by Javascript. Commented Oct 14, 2010 at 9:07
  • "pasword must not more than 10 characters\n" — Why? On a related note: "hard code 3 users" — Why?? These questions are rhetorical of course, I don't think you can find an acceptable rationale for doing this. Commented Oct 14, 2010 at 9:09
  • Oh i used this code just to make a simple dynamic website that's all. It just for my assignment though. Commented Oct 14, 2010 at 16:18

1 Answer 1

1

Well I commented already. Don't do JavaScript and any kind of login!

But I am a nice guy so I'm still trying to help. Try the following (or check out the live example here: http://www.jsfiddle.net/spyAB/1/):

function validate(loginForm) {
    var booValid = true;
    var strErrorMessage = "";
    var minLength = 5;
    var maxLength = 10;

    if (loginForm.pword.value.length < minLength) {
        strErrorMessage = "password must at least 5 characters\n";
        booValid = false;
    }
    else if (loginForm.pword.value.length > maxLength) {
        strErrorMessage = "pasword must not more than 10 characters\n";
        booValid = false;
    }
    if (loginForm.loginid.value != "[email protected]" && loginForm.loginid.value != "[email protected]" && loginForm.loginid.value != "[email protected]") {
        strErrorMessage += "Incorrect Login ID, Please try again\n";
        booValid = false;
    }
    if (!booValid) {
        alert(strErrorMessage);
    }
}

You had one thing wrong: Why not compare the value of the pword field directly to your string instead of going for indexOf? Are you using dropdown boxes? Also I took the liberty to insert += to your strErrorMessage as to concatenate the message with the previous password message. Also your return booValid was unnecessary.

PS: I hope [email protected] are not your real e-mail addresses. Never post your e-mail address in plain text like this or the spam bots will hunt you down.

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

3 Comments

"Don't do it! - But I will still show you how." Cognitive dissonance?
In a way it hurts writing this, of course. But I still think one can learn from mistakes. We could see this alert(strErrorMessage); as a very crude way of "validation", i.e. validating the permitted usernames (god forbid) or validating the password length (...)
Thanks so much for the code above, it works perfect. The code i've got there was from lecturer as i used it to demonstrate a simple dynamic website for my assignment. Although, i've tried just exact as your code above "loginForm.loginid.value != "[email protected]"" but i thought indexOf was better so yea. The email address there was not my real address ay lolz. Anyway thanks so mcuh.

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.