javascript 在输入错误密码时显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25077775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:53:19  来源:igfitidea点击:

display message on wrong password entry

javascripthtmlformsfunction

提问by user3487121

I have a submit button which only works when "victory" is typed into the form. Now I am trying to work on an error message to display "wrong keyword entry" if the text entered into the form field isn't "victory". here is the code

我有一个提交按钮,只有在表单中输入“胜利”时才有效。现在,如果输入到表单字段中的文本不是“胜利”,我正在尝试处理一条错误消息以显示“错误的关键字输入”。这是代码

<form name="input" action="index.html" method="post" onsubmit="return check();">
<input type="text" maxlength="7" autocomplete="off" name="" id="victory">

<br><input type="submit" class="bigbutton"  value="NEXT">

</form>    
<script>

function check(){
if(document.getElementById("victory").value == "victory")
    return true;
else 
   return false;

}

}

采纳答案by ElliotSchmelliot

If I were you I'd add an HTML element to stuff an error into. Then you can style it with CSS however you'd like.

如果我是你,我会添加一个 HTML 元素来填充错误。然后,您可以根据需要使用 CSS 对其进行样式设置。

<div id="error"></div>

Then your function would look something like this:

那么你的函数看起来像这样:

function check(){
    if(document.getElementById("victory").value == "victory")
        return true;
    else 
        document.getElementById("error").innerHTML = "Wrong keyword entry."
        return false;
}

回答by T J

You can simply add the alert in the elsecondition…

您可以简单地在else条件中添加警报......

function check(){
   if(document.getElementById("victory").value == "victory") {
        return true;
   }
   else {    
        alert("wrong keyword entry");
        return false;
   }
 }