开发者

Multiple Login users - javascript

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("jss1@yahoo.com.au") == -1)
 {
    strErrorMessage = "Incorrect Login ID, Please try again\n";
    booValid = false;
 }
 else if(loginForm.loginid.value.indexOf("jss2@yahoo.com.au") == -1)
 {
    strErrorMessage = "Incorrect Login ID, Please try again\n";
    booValid = false;
 }
 else if(loginForm.loginid.value.indexOf("jss3@yahoo.com.au") == -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("jss1@yahoo.com.au") == -1) || loginForm.pword.value开发者_StackOverflow中文版.indexOf("jss1@yahoo.com.au") == -1 || loginForm.pword.value.indexOf("jss1@yahoo.com.au") == -1)
{
strErrorMessage = "Incorrect Login ID, Please try again\n";
booValid=false;
}

Please help!


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 != "jss1@yahoo.com.au" && loginForm.loginid.value != "jss1@yahoo.com.au" && loginForm.loginid.value != "jss1@yahoo.com.au") {
        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 jss1@yahoo.com.au 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜