Search code examples
regexflutterdart

Accept single quote and double quote for password validation in flutter


I want to accept both quotes (',") in a password field validation, but my regex isn't functioning. I've tried a lot of regular expression patterns, but it doesn't work. Could you possibly help me?

I've used both regex:

  • RegExp 1: r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~\":;!?/()+\-\\\$[\]{}|/?>.<,::~^&*()_+=-]).{8,}$'

  • RegExp 2: r"[a-zA-Z0-9!@#$%^&*.,?/{}|()=+-:;'[]<>]"

Example string:

String password = """@Ado'da"foewa""";

These are the conditions I would like to have:

  • At least one uppercase letter
  • At least one lowercase letter
  • At least one number
  • At least one special character from a specific set (including symbols like !.,@#$&*~":;'?/()+-[]{}<>_^ etc.)

Solution

  • You can use multiline string: '''my string'''

    Try: RegExp(r'''^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*['"!@#\$&*~:;!?/()+\-\\\$[\]{}|/?>.<,::~^&*()_+=-]).{8,}$''');

    You forgot a number in this example:

    String password = """@Ado'da"foewa""";