Regular Expression for password

Asked

Viewed 559 times

5

I’m trying to do a strong javascript password check. Initially I want to check if in the string there is the occurrence of 02 numbers in the following conditions:

  1. "12vvv": two consecutive numbers or more of anything returning true
  2. "1a1a": two numbers separated or more returning true
  3. Must return true in all cases where the text contains 02 numbers or more, regardless of its placement.

I got the expression below that returns true only in case 2 ("1a1a"):

/(\d{1}).(\d{1})/.test("a1a1")

But best option was no regular expression:

  function hasTwoNumber(text) {
    var arr        = text.split(""),
        tamanho    = arr.length,
        qtd = 0;

    for (var i = 0; i < tamanho; i++) {
      if (/[0-9]/.test(arr[i])) {
        qtd++;
      }
    }

    return qtd > 1;
  }

console.log(hasTwoNumber("1a1a")); //true
console.log(hasTwoNumber("11aa")); //true

I would like to get that same result with regular expression. Someone knows a regular expression that meets these conditions?

  • Two numbers followed in the middle or end of the true string as well? example a22a or aa22?

  • To make it more clear that it takes the following case: the regular expression must identify if the expression has 02 numbers, necessarily, regardless of whether it is sequential or not. The important thing is to identify 02 numbers.

2 answers

4


A different way:

var a = '1a11aa1'.match(/\d.*\d+/g) != null;
console.log(a);

var b = 'a1a11'.match(/\d.*\d+/g) != null;
console.log(b);

var c = '1aaaa1'.match(/\d.*\d+/g) != null;
console.log(c);

var d = 'aaaa1'.match(/\d.*\d+/g) != null;
console.log(d);

  • 1

    Orraaa! That’s exactly what I want!

  • In that expression gave error: /^[\D]*\d[\D]*\d[\D]*$/.test('a1a11');

  • @adrianosymphony gave no, see again, I edited the answer and added the test at the end.

  • I edited the question to improve the result. I need to force the password to have at least 02 numbers (or more). My intention is that user has 02 numbers in the password or more. It became clearer my intention?

  • @adrianosymphony The new edition changes the meaning of the original question, but I will edit the answer. Just be careful when asking, because it messes up all the answers posted before editing.

  • @adrianosymphony edited, see if it’s working the way you want it now.

  • understood, I will try to avoid these conditions of what is allowed in the next issues.

  • 2

    Perfect, perfectly met my problems! I only adapted in my function with the following expression: !!'1a11aa1'.match(/\d.*\d+/g)

Show 3 more comments

4

You can use the following regex /\d.\d|\d{2}./ she says she should marry a number followed by anything and then another number or (|) two numbers followed by anything.

Examples:

/\d.\d|\d{2}./.test('1bab22') //false, não tem nada depois dos dois números seguidos
/\d.\d|\d{2}./.test('1b1') //true, casa o primeiro padrão (lado esquerdo)
/\d.\d|\d{2}./.test('1baaa') //false
/\d.\d|\d{2}./.test('11') //false
/\d.\d|\d{2}./.test('111') //true, devido ao ponto casar qualquer coisa

If it is necessary to return false for three consecutive numbers exchange the point for \D which means no digit.

/\d\D\d|\d{2}\D/.test('111') //false

Can identify 2 or more numbers with '2aaaa1a'.match(/\d+/g), in which case the approach changes from validate to capture and then check if it matches the expected.

var temNumero = '2aaaa1a'.match(/\d+/g);
if(temNumero.length >=2){
   console.log('válido');
}else{
   console.log('não atende o padrão');
}
  • as commented above, I need you to identify if you have 02 numbers in the text at least in any positions. I couldn’t find anything like that with regular expression.

  • @adrianosymphony a regex failed in some example? can I fix

  • In that condition she fails: /\d.\d|\d{2}./.test('1bab1') . can fix yes :)

  • @adrianosymphony added a correction.

  • 1

    worked right. Thanks for the contribution

Browser other questions tagged

You are not signed in. Login or sign up in order to post.