简体   繁体   English

Javascript Regex密码验证问题

[英]Javascript Regex Password validation issues

I'm currently doing a Javascript password control, which of I have to put alerts out for every single mismatch the password has towards the password requirements, which tells you exactly what's wrong with your input. 我目前正在执行Javascript密码控制,对于密码要求与密码要求不符的每一个不匹配项,我都必须发出警报,以告诉您输入的确切问题。

I've been trying and trying, but never found a solution. 我一直在努力,但从未找到解决方案。

So here's the code: 所以这是代码:

function checkForm(form)
  {

     var re = /^\w{6,10}$/;

    if(!re.test(form.pwd1.value)) {
      alert("Password in-between 6-10 characters");
      form.inputfield.focus();
      return false;
    }

    var re = /^[\w ]+$/;

    if(!re.test(form.pwd1.value)) {
      alert("You need to write something");
      form.inputfield.focus();
      return false;
    }

    var re = /^(?=.*[A-Z])\w{2,}$/;

    if(!re.test(form.pwd1.value)) {
      alert("At least two Upper case");
      form.inputfield.focus();
      return false;
    }


    var re = /^(?=.*[a-z])\w{2,}$/;

    if(!re.test(form.pwd1.value)) {
      alert(" At least two Lower Case letters");
      form.inputfield.focus();
      return false;
    }

    var re = /^(?=.*\d)\w{2,}$/;

    if(!re.test(form.pwd1.value)) {
      alert("At least two numbers");
      form.inputfield.focus();
      return false;
    }

    // validation was successful
    else(alert("Welcome!"));
    return true;
  }

My issue is that the controller settles by one character of each, instead of two, however it need you to input at least two of one of the values eg. 我的问题是,控制器以每个而不是两个为一个字符,但是它需要您输入其中一个值中的至少两个。 either 2x uppercase, 2x lowercase or 2x numbers. 2x大写,2x小写或2x数字。 Which is impossible to not-do when it requires 6 characters. 需要6个字符时,这是不可能做到的。 And that's the weirdest thing, that expression actually works! 这是最奇怪的事情,这种表达实际上有效!

I'd really appreciate if anyone has any input on this. 如果有人对此有任何投入,我将不胜感激。 Thanks! 谢谢!

first of all, it is annoying getting alert box every time but if it is what you want then change your upper lower and number checking pattern; 首先,每次都会出现令人讨厌的警报框,但如果您要更改它,则更改您的上下部和数字检查模式;

^.*[A-Z]{2,}.*$
^.*[0-9]{2,}.*$
^.*[a-z]{2,}.*$

Your 2-Upper-case, 2-Lower-case and 2-Number patterns are wrong. 您的2个大写字母,2个小写字母和2个数字格式是错误的。 You should use something like: 您应该使用类似:

var re = /^.*([A-Z].*){2,}$/;
var re = /^.*([a-z].*){2,}$/;
var re = /^.*([0-9].*){2,}$/;

See this short demo . 请看这个简短的演示

EDIT : 编辑
Please, note that: 请注意:

  • By using \\w you limit the valid password characters to letters, digits, and underscores (more info here ). 通过使用\\w您可以将有效密码字符限制为字母,数字和下划线(更多信息,请参见 )。
  • According to the specs you gave, you need either 2 upper OR 2 lower OR 2 digits. 根据您提供的规格,您需要2个高位或2个低位或2位数字。 This means you cannot alert the user if he fails one of those check, because he might succeed the next. 这意味着如果用户未通过其中一项检查, 则无法警告用户,因为他可能会通过下一项 You have to make all three checks and the alert the user if ALL three fail. 您必须进行所有三个检查,并在所有三个失败的情况下向用户发出警报。
  • If the user enters a 6-10 letter password containing non- \\w characters (eg "abCD56-"), he will be alerted that "the password should be between 6 and 10 characters", while his password IS 6-10 characters (just not the characters you want), so the message would be kind of misleading in this case. 如果用户输入包含非\\w字符的6-10个字母的密码(例如“ abCD56-”),则会提示他“密码应介于6到10个字符之间”,而他的密码 6-10个字符(而不是您想要的字符),因此在这种情况下,该消息可能会产生误导。

I hope that helps. 希望对您有所帮助。

There is a pattern you use in your code that does something different from what you likely intend: 您的代码中使用了一种模式,该模式与您可能的意图有所不同:

    var re = /^(?=.*\d)\w{2,}$/;

The lookahead, (?=.*\\d) , does not consume any characters in the string. 前瞻(?=.*\\d)不会占用字符串中的任何字符。 It looks ahead at the point of the anchor to match the pattern .*\\d . 它在锚点处向前看,以匹配模式.*\\d The lookahead will return a successful match if there is one digit in the string (ie, any number of characters followed by a digit). 如果字符串中包含一位数字(即,任意数量的字符后跟一位数字),则lookahead将返回成功的匹配项。

Conceptually, once the lookahead returns true, the parser will attempt to match \\w{2,}$ , which matches any number of word characters (letters, numbers, and _ ) followed by the end of the string. 从概念上讲,一旦前瞻返回true,解析器将尝试匹配\\w{2,}$ ,后者匹配任意数量的单词字符(字母,数字和_ ),后跟字符串的结尾。

The result is that the pattern will match any string comprised of 2 or more word characters that contains at least one digit, rather than the intended two. 结果是该模式将匹配任何包含2个或更多单词字符的字符串,这些字符至少包含一个数字,而不是预期的两个数字。

As a correction, you have several options. 作为更正,您有几种选择。 Since your first pattern ensures that the password is a proper length, you can replace this expression with: 由于您的第一个模式可确保密码长度正确,因此可以将以下表达式替换为:

var re = /\d.*\d/;

This will find a digit followed by any sequence of characters, followed by another digit (note the removal of the ^ and $ anchors, since the first digit can occur anywhere in the string). 这将找到一个数字,后跟任意字符序列,然后是另一个数字(请注意,删除了^$锚,因为第一个数字可以出现在字符串的任何位置)。

The same concept can be applied to the other patterns that require two symbols: 相同的概念可以应用于需要两个符号的其他模式:

var re = /^(?=.*[A-Z])\w{2,}$/; becomes var re = /[A-Z].*[A-Z]/;
var re = /^(?=.*[a-z])\w{2,}$/; becomes var re = /[a-z].*[a-z]/;

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

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