我想通过javascript过滤密码,但是当我使用该代码时,它会弹出警报,不管我放的是什么

问题描述 投票:0回答:2

function check() {
  var passwd = document.getElementById('pass');
  if (passwd.length >= 8 && passwd.length <= 20 && !(/(.)\1{3}/i.test(passwd)) && (/[^A-Za-z]/.test(passwd)) && !(/0123|1234|2345|3456|4567|5678|6789|7890|8901|9012/.test(passwd)) && !(/qwer|wert|erty|rtyu|tyui|yuio|uiop|asdf|sdfg|dfgh|fghj|ghjk|hjkl|zxcv|xcvb|cvbn|vbnm|QWER|WERT|ERTY|RTYU|TYUI|YUIO|UIOP|ASDF|SDFG|DFGH|FGHJ|GHJK|HJKL|ZXCV|XCVB|CVBN|VBNM/.test(passwd)) && !(/\s/.test(passwd)) && !(/access14|americanidol|baseball|baseball1|bigdaddy|blink182|butthead|cocacola|computer|corvette|cowboys|danielle|dolphins|einstein|firebird|football|football1|iloveyou|iloveyou1|internet|jennifer|jordan23|liverpool|liverpool1|marlboro|maverick|melanie|michelle|midnight|mistress|mountain|myspace1|password|password1|princess1|qwertyui|redwings|rush2112|samantha|scorpion|slipknot1|srinivas|startrek|starwars|sunshine|superman|superman1|swimming|trustno1|victoria|whatever|passwort|passwort1|frankfurt|fussball/i.test(passwd))) {

    return true;
  } else {
    alert("Some of your information are incorrect");
    return false;

  }
}
<input id="pass" type="text" />
<button onclick="console.log(check())">Check</button>
javascript html regex html5 webpage
2个回答
4
投票

这是因为在您的代码中,您将passwd设置为input元素:

var passwd = document.getElementById('pass');

您需要将其设置为input元素的值:

var passwd = document.getElementById('pass').value;

1
投票

你应该改变这一行

var passwd = document.getElementById('pass');

有了这个:

var passwd = document.getElementById('pass').value;

因为在另一种情况下,您将始终获得输入节点,而不是它的值(您真正想要的)。

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.