web-dev-qa-db-fra.com

Comment implémenter une expression rationnelle pour la validation du mot de passe dans Swift?

Je souhaite implémenter une validation regex pour les mots de passe dans Swift? J'ai essayé la regex suivante, mais sans succès

([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([a-z]*){6,15}

Mon exigence est la suivante: Le mot de passe doit comporter plus de 6 caractères, avec au moins un caractère majuscule, numérique ou spécial.

31
Vaibhav Jhaveri

La regex est

(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ+=_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_]))[A-Za-z0-9-!@#$%&*ˆ+=_]{6,15}
8
Vaibhav Jhaveri

Vous pouvez utiliser Regex pour vérifier votre force mot de passe

^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$

Explication Regex: -

^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&*])            Ensure string has one special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8}                      Ensure string is of length 8.
$                         End anchor.

Source - Lien Rublar

60
Anand Nimje

essayez avec celui-ci pour Le mot de passe doit comporter plus de 6 caractères, avec au moins un caractère majuscule, numérique ou spécial

^.*(?=.{6,})(?=.*[A-Z])(?=.*[a-zA-Z])(?=.*\\d)|(?=.*[!#$%&? "]).*$

^ assert position at start of the string
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.{6,}) Positive Lookahead - Assert that the regex below can be matched
.{6,} matches any character (except newline)
Quantifier: {6,} Between 6 and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.*[A-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[A-Z] match a single character present in the list below
A-Z a single character in the range between A and Z (case sensitive)
(?=.*[a-zA-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
(?=.*\\d) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
2nd Alternative: (?=.*[!#$%&? "]).*$
(?=.*[!#$%&? "]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[!#$%&? "] match a single character present in the list below
!#$%&? " a single character in the list !#$%&? " literally (case sensitive)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string

https://regex101.com/#javascript

plus cela vous pouvez essayer ....

Minimum 8 caractères, au moins 1 alphabet et 1 chiffre:

"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"

Minimum 8 caractères, au moins 1 alphabet, 1 chiffre et 1 caractère spécial:

"^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"

Minimum 8 caractères, au moins 1 alphabet majuscule, 1 alphabet minuscule et 1 chiffre:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$"

Minimum 8 caractères, au moins 1 alphabet majuscule, 1 alphabet minuscule, 1 chiffre et 1 caractère spécial:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[d$@$!%*?&#])[A-Za-z\\dd$@$!%*?&#]{8,}"

Minimum 8 et maximum 10 caractères au moins 1 alphabet majuscule, 1 alphabet minuscule, 1 chiffre et 1 caractère spécial:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&#])[A-Za-z\\d$@$!%*?&#]{8,10}"
40
Nazmul Hasan
public func isValidPassword() -> Bool {
    let passwordRegex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!@#$%^&*()\\-_=+{}|?>.<,:;~`’]{8,}$"
    return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}

Si vous avez besoin d'une solution rapide. C'est la validation d'un mot de passe avec regex. Copier/coller dans un fichier d'aide ou un fichier d'extension et l'utiliser.

15
tBug