Ensuring that a number is present in a string?

1 vue (au cours des 30 derniers jours)
Andy
Andy le 10 Avr 2014
Commenté : Andrew Newell le 10 Avr 2014
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
Andy
Andy le 10 Avr 2014
Modifié(e) : Andy le 10 Avr 2014
RE:
I am hoping to do something similar to what I did for the function that ensures a capital letter is present in the string. It will look better when written out similarly, in my opinion. Here is what I did for that function:
function [CAPS_check]=CAPS_check(password)
%CAPS_check ensures there is at least one capital letter in the password
%all(lower(password)==password) equal to one, then function has at least one CAP
CAPS_check=all(lower(password)==password);
end

Connectez-vous pour commenter.

Réponse acceptée

Andrew Newell
Andrew Newell le 10 Avr 2014
Modifié(e) : Andrew Newell le 10 Avr 2014
This can be solved nicely using MATLAB regular expressions. The idea is to look ahead from the start of the line to see if each of the criteria are met, and then to return any strings that match these criteria. If an empty string is returned, it is not a valid password.
function tf = isValidPasswd(str)
anchorToStartOfLine = '^';
atLeastEightChars = '(?=.{8,})'; % I'm assuming that any characters are allowed
atLeastOneLC = '(?=.*?[a-z])';
atLeastOneUC = '(?=.*?[A-Z])';
atLeastOneDigit = '(?=.*?\d)';
matchAll = '.*';
passwdCriteria = [anchorToStartOfLine,atLeastEightChars,atLeastOneLC,atLeastOneUC,atLeastOneDigit,matchAll];
tf = ~isempty(regexp(str,passwdCriteria,'once'));
  2 commentaires
Andy
Andy le 10 Avr 2014
Thanks for the help! I was able to use what you did for "atleastOneDigit" and turn it into a function to satisfy the assignment. This is actually part of my homework assignment but I couldn't for the life of me figure it out. Thanks again!!
Andrew Newell
Andrew Newell le 10 Avr 2014
Andy, I'm glad to hear that you made your own version for the assignment.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 10 Avr 2014
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
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
Andrew Newell le 10 Avr 2014
Modifié(e) : Andrew Newell le 10 Avr 2014
Nice!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by