Patrón Regex para una contraseña

I need a regex pattern that validates a password format. The rules are:

  • Minimum 8 chars in total
  • al menos dos letras
  • at least two digits or symbols

Se me ocurrió lo siguiente: /((?=.*[0-9\@\&#\$\?\%!\|(){}[]])(?=.*[a-zA-Z]).{8,})/

It will look if both occure once, but I need it toch validate if they occur at least twice. If I add the {2,} Me gusta esto: /((?=.*[0-9\@\&#\$\?\%!\|(){}[]]{2,})(?=.*[a-zA-Z]{2,}).{8,})/

Then the following doesnt work for example: a1a1a1a1a1

¿Puede alguien ayudarme?

preguntado el 28 de agosto de 12 a las 09:08

2 Respuestas

This is how you do it, using positive lookaheads: http://regex101.com/r/uW0yI4

/^(?=.*[a-z].*[a-z])(?=.*[!"#...\d].*[!"#...\d]).{8,}$/gmi

Solo reemplaza !"#... with all the symbols you want to match.

Note: the multiline flag might be unnecessary for your applications.

Respondido 28 ago 12, 09:08

This works, only one small remark, it will accept unallowed chars like ~ too. How can I stop that? - user1629636

Cambie el . dot to a character class containing what you want to allow. For example: /^(?=.*[a-z].*[a-z])(?=.*[!"#...\d].*[!"#...\d])[a-z\d]{8,}$/gmi - Firas Dib

This should give you what you're after:

^((?=(.*[\d0-9\@\&#\$\?\%!\|(){}[\]]){2,})(?=(.*[a-zA-Z]){2,}).{8,})$

Respondido 28 ago 12, 09:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.