0

This expression must adhere to specific rules:

1.- Between 2 and 8 characters total.
2.- Start with uppercase.
3.- Contain both lowercase and digits.

The first and second should be easy, but I can't get the third one to work.

This is the expression I came up with

([A-Z]+[A-Za-z0-9]*){2,8}  

But it returns incorrect responses. Regular expressions are far from my forte, and this is the first time I had to use them outside of class.

This is my code, if it helps

    var expresion = /([A-Z]+[A-Za-z0-9]*){2,8}/;
    var re = new RegExp(expresion); 
    var t = $('#code').val(); 
    if (re.test(t)) { 
        console.log(t+' works');
    } else {
        console.log(t+' not working');
    }
TianRB
  • 671
  • 1
  • 7
  • 22
  • Possible duplicate of [PHP regular expression for strong password validation](http://stackoverflow.com/questions/2637896/php-regular-expression-for-strong-password-validation) – dawg Mar 27 '17 at 00:40
  • Per rules on [tag:regex] tag, please also tag with [tag:javascript]. In an unrelated comment, if this is a real password validation situation, please don't - it makes passwords _less_ secure. – Amadan Mar 27 '17 at 00:41
  • I don't think this is password validation. This looks more like identifier naming conventions (although those usually allow underscore as well). – Bryce Wagner Mar 27 '17 at 00:42
  • 2
    Given item 2 and 3, a total of 3 characters is the minimum: 1, start with upper case, 2contain both lowercase and digit -- that is at least 3 characters. – dawg Mar 27 '17 at 00:43
  • 2
    with allowed 2 characters, how can you meet the requirements of starting with uppercase and containing both lowercase and digits? – Shiping Mar 27 '17 at 00:43
  • Are characters other than letters and numbers allowed: `"Aa1$#!@#"`? – nnnnnn Mar 27 '17 at 01:20

2 Answers2

1

Use look aheads that comport to each condition:

/^(?=[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.{2,8}$)(.*)/m

Demo

(As stated in comments, your target pattern is a minimum of 3 characters with the other conditions...)

dawg
  • 98,345
  • 23
  • 131
  • 206
1

This should fit your literal requirements (however, as comments state, they don't really make sense):

^(?=.{2,8}$)(?=.*[0-9])(?=.*[a-z])[A-Z][a-zA-Z0-9]*$

First, you need to anchor your match with ^ (start of string) and $; otherwise you can just be picking up a matching substring, which will mess up your requirements.

Second, we use lookahead to validate several individual points: the string contains between 2 and 8 characters before it ends, the string contains a digit.

Third, we use the character classes to validate that it starts with an uppercase, and continues with a mix of uppercase, lowercase and digits.

EDIT: Forgot the lowercase requirement, thanks nnnnnn. And you are right, your version is better.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • This regex accepts a string like `"A123"` with no lowercase letters. Also, isn't the lookahead for length more complicated and less efficient than just putting `{1,7}` at the end: `^(?=.*[0-9])(?=.*[a-z])[A-Z][a-zA-Z0-9]{1,7}$`? – nnnnnn Mar 27 '17 at 01:17