tags:

views:

32

answers:

1

Hi guys,

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!

A: 

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.

moontear
"Don't do it! - But I will still show you how." Cognitive dissonance?
Tomalak
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 (...)
moontear
@moontear: 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.
James1