2

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,938
  • 1
  • 18
  • 45
  • 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. – Truong 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 – Truong 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. – Truong Jul 02 '21 at 14:27
  • See also this code https://www.codevscolor.com/dart-check-character-uppercase – Truong Jul 02 '21 at 14:32

4 Answers4

9
  • 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
  • 299
  • 3
  • 11
  • 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
7
extension StringValidators on String {
  bool get containsUppercase => contains(RegExp(r'[A-Z]'));
  bool get containsLowercase => contains(RegExp(r'[a-z]'));
}
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,880
  • 1
  • 11
  • 18
1
void main() {
  solve("coDE");
}

String solve(String s) {
  // your code here
  List _a = s.split("");
  String _b = "";
  List _x = [];
  List _y = [];
  for(var i in _a){
    if(i.toString() == i.toString().toUpperCase()){
      _x.add(i);
    }else{
      _y.add(i);
    }
  }
  
  if(_x.length == _y.length){
    _b = _a.join().toLowerCase();
  }else if(_x.length > _y.length){
     _b = _a.join().toUpperCase();
  }else if(_x.length < _y.length){
     _b = _a.join().toLowerCase();
  }
  return "$_b";
}

OR 

String solve2(String str) {
  return RegExp(r'[A-Z]').allMatches(str).length >
          RegExp(r'[a-z]').allMatches(str).length
      ? str.toUpperCase()
      : str.toLowerCase();
}
Jamirul islam
  • 502
  • 6
  • 10