'how use regexp in flutter?

enter image description hereI want this format.see at photo. I use this code

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

This code is ok for java but not for dart.I don't know why?



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Suraj Rao