Ensuring that a number is present in a string?
Show older comments
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 Comment
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Apr 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 Comments
Walter Roberson
on 10 Apr 2014
Edited: Andrew Newell
on 10 Apr 2014
~any(cellfun(@isempty, regexp(str, {'.{8,}', '[A-Z]', '[a-z]', '\d'}, 'match', 'once')))
Andrew Newell
on 10 Apr 2014
Edited: Andrew Newell
on 10 Apr 2014
Nice!
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!