Regex for alphanumeric password, with at least 1 number and character

前端 未结 6 1938
礼貌的吻别
礼貌的吻别 2020-12-18 13:02

Need help with a regex for alphanumeric password, with at least 1 number and character, and the length must be between 8-20 characters.

I have this but doesn\'t seem

相关标签:
6条回答
  • 2020-12-18 13:28

    Wouldn't it be better to do this validation with some simple string functions instead of trying to shoehorn a difficult to validate regex into doing this?

    0 讨论(0)
  • 2020-12-18 13:38

    This code is for javascript

    // *********** ALPHA-Numeric check ***************************
    function checkAlphaNumeric(val)
    {
        var mystring=new String(val)
    
        if(mystring.search(/[0-9]+/)==-1) // Check at-leat one number
        {
            return false;
        }
        if(mystring.search(/[A-Z]+/)==-1 && mystring.search(/[a-z]+/)==-1) // Check at-leat one character
        {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-18 13:41

    Something like this will be closer to your needs. (I didn't test it, though.)

    Regex test = new Regex("^(?:(?<ch>[A-Za-z])|(?<num>[9-0])){8,20}$");
    Match m = test.Match(input);
    if (m.Success && m.Groups["ch"].Captures.Count > 1 && m.Groups["num"].Captures.Count > 1)
    {
      // It's a good password.
    }
    
    0 讨论(0)
  • 2020-12-18 13:49
    ^(?=.{8,20}$)(?=.*[0-9])(?=.*[a-zA-Z]).*
    

    ? :)

    0 讨论(0)
  • 2020-12-18 13:50

    Why not just use a handful of simple functions to check?

    checkPasswordLength( String password);
    checkPasswordNumber( String password);
    

    Maybe a few more to check for occurrences of the same character repeatedly and consecutively.

    0 讨论(0)
  • 2020-12-18 13:53

    If you take a look at this MSDN link, it gives an example of a password validation RegEx expression, and (more specifically) how to use it in ASP.NET.

    For what you're looking to accomplish, this should work:

        (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$
    

    This requires at least one digit, at least one alphabetic character, no special characters, and from 8-20 characters in length.

    0 讨论(0)
提交回复
热议问题