1

I need to validate password entered by user and check if the password contains at least one uppercase and one lowercase char in Dart.

I wrote this String extension:

extension StringValidators on String {
  bool containsUppercase() {
    // What code should be here?
  }

  bool containsLowercase() {
    // What code should be here?
  }
}

And use it like this:

final text = passwordTextController.text;
final isValid = text.containsUppercase() && text.containsLowercase();

Is there any regexp for this purpose? Or it should be plain algorithm? Please help me to find out the elegant way. Thanks!

Mol0ko
  • 2,216
  • 1
  • 14
  • 34
  • Traverse the string character by character from start to end. Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter. – Misa Jul 02 '21 at 14:18
  • See also this question https://stackoverflow.com/questions/40336374/how-do-i-check-if-a-java-string-contains-at-least-one-capital-letter-lowercase. It is for Java. Hope you can find an analogy in Flutter – Misa Jul 02 '21 at 14:20
  • @Misa could you add an answer with Dart code? – Mol0ko Jul 02 '21 at 14:21
  • Unfortunately I don't know Dart programming language. I know Java. Anyway I guess the implementation behind ```Character.isUpperCase(ch)``` is the ASCII value as I mention in the first comment. – Misa Jul 02 '21 at 14:27
  • See also this code https://www.codevscolor.com/dart-check-character-uppercase – Misa Jul 02 '21 at 14:32

3 Answers3

4
  • Minimum 1 Upper case,
  • Minimum 1 lowercase,
  • Minimum 1 Numeric Number,
  • Minimum 1 Special Character,
  • Common Allow Character ( ! @ # $ & * ~ )
bool validateStructure(String value){
        String  pattern = r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
        RegExp regExp = new RegExp(pattern);
        return regExp.hasMatch(value);
  }
S4nj33v
  • 181
  • 1
  • 9
  • It works great but I don't need to validate all of this conditions, only lowercase and uppercase characters. Anyway thanks a lot – Mol0ko Jul 02 '21 at 14:49
2

For only minimum 1 upper and minimum 1 Lower only, you could use this RegEx:

RegExp regEx = new RegExp(r"(?=.*[a-z])(?=.*[A-Z])\w+");
String a = "aBc";
String b = "abc";
String c = "ABC";
print("a => " + regEx.hasMatch(a).toString());
print("b => " + regEx.hasMatch(b).toString());
print("c => " + regEx.hasMatch(c).toString());

Expected Result:

I/flutter (10220): a => true
I/flutter (10220): b => false
I/flutter (10220): c => false

Reusable

extension StringValidators on String {

  meetsPasswordRequirements() {
    RegExp regEx = new RegExp(r"(?=.*[a-z])(?=.*[A-Z])\w+");
    return regEx.hasMatch(this);
  }

}

Use

final isValid = text.meetsPasswordRequirements();
daddygames
  • 1,740
  • 1
  • 10
  • 18
2
extension StringValidators on String {
  bool get containsUppercase => contains(RegExp(r'[A-Z]'));
  bool get containsLowercase => contains(RegExp(r'[a-z]'));
}