Javascript 创建一个“简单”的密码验证字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7974040/
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
Creating a "simple" password validation field
提问by Josh I
I'm trying to make a password field for a webpage. So far I have:
我正在尝试为网页创建密码字段。到目前为止,我有:
<form name="PasswordField" action="">
Password:
<input type="password" name="password">
<input type="button" value="Log in">
</form>
Pathetic I know. It doesn't have to be fancy, I just need it to "get" the password from the textbox and match it against the password for the page. I'm assuming I can use an if-else?
可怜我知道。它不必很花哨,我只需要它从文本框中“获取”密码并将其与页面的密码进行匹配。我假设我可以使用if-else?
*Code for get password from textbox when the "Log in" button is pressed here*
if (password = "rawr")
{alert('Correct!')}
else
{alert('Wrong Password')}
Sadly I've been fooling with this for hours. I tried functions, too, and that didn't seem to work (for me) either.
可悲的是,我一直在愚弄这个好几个小时。我也尝试过函数,但这似乎也不起作用(对我来说)。
采纳答案by Nick Rolando
If you go that route, you need to put the validation inside a function that gets called in the onclick
event of your button. Also to access the password <input
node in js, you can give it an id
and use document.getElementById(id)
. Also, =
is an assignment operator. Use ==
for comparison :)
如果你走那条路,你需要把验证放在一个函数中,这个函数在onclick
你的按钮事件中被调用。同样要访问<input
js 中的密码节点,您可以给它一个id
并使用document.getElementById(id)
. 此外,=
是一个赋值运算符。使用==
比较:)
<head>
<script type="text/javascript">
function isValid(){
var password = document.getElementById('password').value;
if (password == "rawr")
{alert('Correct!')}
else
{alert('Wrong Password')}
}
</script>
</head>
<form name="PasswordField" action="">
Password:
<input type="password" id="password" name="password">
<input type="button" value="Log in" onclick="isValid();">
</form>
Or an even easier way would be to pass the password DOM node as an argument to the function:
或者更简单的方法是将密码 DOM 节点作为参数传递给函数:
<head>
<script type="text/javascript">
function isValid(myNode){
var password = myNode.value;
if (password == "rawr")
{alert('Correct!')}
else
{alert('Wrong Password')}
}
</script>
</head>
<form name="PasswordField" action="">
Password:
<input type="password" id="password" name="password">
<input type="button" value="Log in" onclick="isValid(this);">
</form>
回答by Ariel
Is this what you are looking for?
这是你想要的?
document.forms['PasswordField'].elements['password'].value
回答by K2so
I used jquery and here's my solution:
我使用了 jquery,这是我的解决方案:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("input[name='login']").click(function() {
var s = $("input[name='password']").val();
if(s == "rawr") {alert('Correct!')}
else {alert('Wrong Password')}
});
});
</script>
</head>
<body>
<form name="PasswordField" action="">
Password:<input type="password" name="password">
<input type="button" value="Log in" name="login">
</form>
</body>
</html>