Patrón Regex para una contraseña
Frecuentes
Visto 1,250 veces
2
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?
2 Respuestas
3
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
0
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 regex validation passwords or haz tu propia pregunta.
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