Ensuring that a number is present in a string?
Afficher commentaires plus anciens
I am writing a code that asks a user for a password, and it must satisfy the following conditions:
- at least 8 characters in length,
- have at least one capital letter, and one lowercase
- include a number
I have the top two figured out but I don't know how to write a function for the third. It is all in a "while" loop and a value of 1 is assigned to each condition... When all 3 are met, the "while" loop stops repeating.
How can I write the function that will determine if there is a number in the password the user inputs?
Thanks for the help!
1 commentaire
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 10 Avr 2014
1 vote
all(lower(password)==password) implies no capital letters. It does not, however, establish that any lowercase letters were used: the password might contain no alphabetic letters at all.
any(password >= '0' & password <= '9') is one of the ways to check for digits.
You should be considering using regexp() instead of looping.
2 commentaires
Walter Roberson
le 10 Avr 2014
Modifié(e) : Andrew Newell
le 10 Avr 2014
~any(cellfun(@isempty, regexp(str, {'.{8,}', '[A-Z]', '[a-z]', '\d'}, 'match', 'once')))
Andrew Newell
le 10 Avr 2014
Modifié(e) : Andrew Newell
le 10 Avr 2014
Nice!
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!