简体   繁体   English

JavaScript我测试密码的功能不起作用

[英]JavaScript my function to test a password doesn't work

function demoMatchClick() {
  var validString = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/
  var re = new RegExp(validString);
  if (document.form1.subject.value.test(re)) {
    alert("Successful match");
  } else {
    alert("No match");
  }
}

<INPUT TYPE=SUBMIT VALUE="Replace" ONCLICK="demoReplaceClick()">

I can't get it to popup an Alert to pop up 我不能让它弹出警报弹出

I want these rules to be enforced 我希望强制执行这些规则

•Not have upper-case letters. 
•Begin with a letter. 
•Have at least 1 digit(s) not at the beginning and end. 
•Have up to 8 alphanumeric 
•Does NOT have any symbols like @#$ characters (symbols like !@#$%^&*()-+).

I am using a button to execute the code for now. 我正在使用一个按钮来执行代码。

Here is the modified code: 这是修改后的代码:

function demoMatchClick(input) {
  var validString = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/;
  if (validString.test(input)) {
    alert("Successful match");
  } else {
    alert("No match");
  }
}


demoMatchClick("hello world");

validString variable is already a RegExp object and you can use it directly, additionally .test() method belongs to regex object not to string. validString变量已经是一个RegExp对象,你可以直接使用它。另外.test()方法属于正则表达式对象而不是字符串。

Well, I suppose this regex suits your rules... 好吧,我想这个正则表达式适合你的规则......

var rules = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/;

But I think there are several issues with your code, which I'd like to point out. 但我认为您的代码存在一些问题,我想指出。 Don't take it as a personal offense, please: believe me, I'm actually saving you a LOT of time and nerves. 请不要将其视为个人冒犯,请:相信我,我实际上为你节省了大量的时间和精力。

First, there's a standard rule: each function should do only one thing - but do it really well (or so they say, these perfectionists!). 首先,有一个标准规则:每个功能应该只做一件事 - 但做得很好(或者他们说,这些完美主义者!)。 Your code is too tightly coupled with DOM extraction: I was really surprised when it failed to work when pasted in my environment! 你的代码与DOM提取过于紧密联系:当我粘贴在我的环境中时,我感到非常惊讶! Only then I noticed that document.forms call. 只有这时我才注意到document.forms调用。 It's not really needed here: it's sufficient to build a function taking one parameter, then call this function with the value extracted somewhere else. 这里不需要它:构建一个带一个参数的函数就足够了,然后用其他地方提取的值调用这个函数。 This way, btw, you can easily separate the causes of errors: it would be either in DOM part, or within the function. 通过这种方式,顺便说一句,您可以轻松地分离错误的原因:它可以是DOM部分,也可以是函数内部。

Second, Regexes are really very close to be considered first-class citizens in JavaScript (not so as in Perl, but still much closer than in some other languages). 其次,正则表达式非常接近被认为是JavaScript中的一等公民(不像在Perl中那样,但仍然比其他语言更接近)。 That means you can write the regex literals as is, and use it later - without new Regexp constructs. 这意味着你可以按原样编写正则表达式文字,并在以后使用它 - 没有新的Regexp结构。

With all that said, I'd write your code as... 尽管如此,我还是把你的代码写成......

function validatePassword(password) {
  var rules = /^[a-z](?=[a-z]*[0-9])[a-z0-9]{0,6}[a-z]$/;
  return rules.test(password);
}

... then use it by something like ... ...然后通过类似的方式使用它...

var password = document.form1.subject.value;
alert( validatePassword(password) ? 'Success! :)' : 'Failure... :(' );

PS And yes, Riccardo is right: set too strict rules for passwords - and suffer the consequences of narrowing the range of search for an attacker. PS并且是的,Riccardo是对的:为密码设置了太严格的规则 - 并且遭受缩小搜索攻击范围的后果。 And it's quite easy to see the validation rules set in Javascript: even obfuscators won't help much. 并且很容易看到Javascript中设置的验证规则:即使是混淆器也无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM