Home > Software design >  How to create a validation rule with mixed case and number in Flutter Dart
How to create a validation rule with mixed case and number in Flutter Dart

Time:10-10

I want to create a validation rule in the Flutter Validator. For reference, I want to create validator functions like the mixedCase() and numbers() functions in Laravel

Roughly explained, something like this

  1. Minimum 8 characters
  2. A mixture of uppercase (A) and lowercase (a) letters
  3. There must be at least 1 number

Example

Passwords1 = true;

passwords1 = false;

passwordss = false;

How to make this, is it possible?
I only know a few conditions right now in the if-else like:
if (value == null || value.isEmpty) if (value.length < 8)

I also read some references like here, but I don't understand the pattern of the (for example) String patttern = r'(^(?:[ 0]9)?[0-9]{10,12}$)';

Can I write the rules as I want above into an if-else code block like this?

validator: (value) {
  if (value == null || value.isEmpty) {
    return 'Please fill the confirm password field';
  } else if (value != passwordController.text) {
    return 'Password doesn\'t match';
  }
  return null;
}

Please help or advice, thank you

CodePudding user response:

Yes, you can use if-else to do it but with a little trick, I already did it and here is my code:

// Create an enum containing warning information, you can modify your warning here
enum PasswordError {
  upperCase('Must contain at least one uppercase'),
  lowerCase('Must contain at least one lowercase'),
  digit('Must contain at least one digit'),
  eigthCharacter('Must be at least 8 characters in length'),
  specialCharacter('Contain at least one special character: !@#\\\$&*~');

  final String message;

  const PasswordError(this.message);
}

// Validate and return all errors as a List
//
// Here are the patterns and what it means:
// r'^
//   (?=.*?[A-Z])      // should contain at least one upper case
//   (?=.*?[a-z])      // should contain at least one lower case
//   (?=.*?[0-9])      // should contain at least one digit
//   (?=.*?[!@#\$&*~]) // should contain at least one Special character
//   .{8,}             // Must be at least 8 characters in length
// $
//
// Source: https://stackoverflow.com/questions/56253787/how-to-handle-textfield-validation-in-password-in-flutter
List<PasswordError> passwordValidator(String password) {
  List<PasswordError> errors = [];
  if (!RegExp(r'[A-Z]').hasMatch(password)) {
    errors.add(PasswordError.upperCase);
  }
  if (!RegExp(r'[a-z]').hasMatch(password)) {
    errors.add(PasswordError.lowerCase);
  }
  if (!RegExp(r'[0-9]').hasMatch(password)) {
    errors.add(PasswordError.digit);
  }
  if (!RegExp(r'[!@#\$&*~]').hasMatch(password)) {
    errors.add(PasswordError.specialCharacter);
  }
  if (!RegExp(r'.{8,}').hasMatch(password)) {
    errors.add(PasswordError.eigthCharacter);
  }

  return errors;
}

Then you can use it for validator:

validator: (password) {
  if (password != null) {
    // Get all errors
    final validators = passwordValidator(password);

    // Returns null if password is valid
    if (validators.isEmpty) return null;
     
    // Join all errors that start with "-"
    return validators
        .map((e) => '- ${e.message}')
        .join('\n');
  }

  return null;
},
  • Related