2 回答
TA贡献1871条经验 获得超13个赞
The issue is because of the iteration you are doing even after finding if entered user is valid user. In simple terms, putting a break statement solves your problem.
See the snippet below:
let userName=document.getElementById("input1");
let mailId=document.getElementById("input2");
var out=[{Name:"dhanam",mail:"dhanamram98@gmail.com"},
{Name:"alamelu",mail:"alamu98@gmail.com"}];
function input()
{
var input=userName.value;
var output=mailId.value;
var created=[{Name:input,mail:output}];
return created
}
function output()
{
var inp=input();
for(var i=0;i<inp.length;i++)
{
for(var j=0;j<out.length;j++)
{
console.log(inp[i].Name, inp[i].mail);
console.log(out[j].Name, out[j].mail);
if((inp[i].Name== out[j].Name)&&(inp[i].mail==out[j].mail))
{
document.getElementById("out1").innerText="welcome user";
break;
}
else{
document.getElementById("out1").innerText="Invalid user";
}
}
}
}
var but=document.getElementById("out");
but.addEventListener("click",output);
注意:这不是验证凭据的最佳实践,也避免使用 var,使用 let,const 代替
TA贡献1796条经验 获得超4个赞
const accounts = [
{
name:"dhanam",
mail:"dhanamram98@gmail.com"
},
{
name:"alamelu",
mail:"alamu98@gmail.com"
}
]
function output() {
const nameNode = document.getElementById("input1")
const mailNode = document.getElementById("input2")
const name = nameNode.value
const mail = mailNode.value
const found = accounts.find(a => a.name === name && a.mail === mail)
if (found) {
document.getElementById("out1").innerText="welcome user";
} else {
document.getElementById("out1").innerText="Invalid user";
}
}
var but=document.getElementById("out");
but.addEventListener("click",output);
添加回答
举报