1

enter image description here I want the format shown in the photo. I used this code

RegExp(r'^(?=.*[0-9])(?=\\S+$).{8,40}$').hasMatch(text).

This code is ok for Java but not for Dart. Why is that?

TarHalda
  • 1,050
  • 1
  • 9
  • 27
H Zan
  • 359
  • 1
  • 4
  • 16

2 Answers2

7

My guess is that double-backslashing might be unnecessary, and:

^(?=.*[0-9])(?=\S+$).{8,40}$

might simply work.


Maybe, you might want to a bit strengthen/secure the pass criteria, maybe with some expression similar to:

(?=.*[0-9])(?=.*[A-Za-z])(?=.*[~!?@#$%^&*_-])[A-Za-z0-9~!?@#$%^&*_-]{8,40}$

which allows,

  • a minimum of one digit,
  • a minimum of one upper/lower case, and
  • at least one of these chars: ~!?@#$%^&*_-

If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Reference

Community
  • 1
  • 1
Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thanks Emma.This is work.Where i look regexp documentation in dart.I don't understand how work.Thanks again – H Zan Sep 16 '19 at 03:16
  • 3
    The reason this works, and the original does not, is that you are using a *raw* string in Dart (the "r" prefix), and you weren't in Java. In a raw string, interpolations and *escapes* are disabled so your `\\S` is read literally by the RegExp parser as an escaped backslash and a capital "S". If you write only `\S`, the RegExp parser will see it as the non-whitespace escape code instead. You need the two backslashes in Java (or in non-raw Dart strings) because the *string* parser converts `\\S` to `\S`, and then the RegExp parser sees `\S` as needed. – lrn Sep 16 '19 at 06:06
1

Maybe there is your entire solution function for checking valid constraint of the password as well as using regex expression in Dart.

String? validatePassword(String? value) {
        String missings = "";
        if (value!.length < 8) {
          missings += "Password has at least 8 characters\n";
        }

        if (!RegExp("(?=.*[a-z])").hasMatch(value)) {
          missings += "Password must contain at least one lowercase letter\n";
        }
        if (!RegExp("(?=.*[A-Z])").hasMatch(value)) {
          missings += "Password must contain at least one uppercase letter\n";
        }
        if (!RegExp((r'\d')).hasMatch(value)) {
          missings += "Password must contain at least one digit\n";
        }
        if (!RegExp((r'\W')).hasMatch(value)) {
          missings += "Password must contain at least one symbol\n";
        }

        //if there is password input errors return error string
        if (missings != "") {
          return missings;
        }

        //success
        return null;
 }

there's my result image

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
InnerPeace
  • 31
  • 3