Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dart : How to check if password contains all required characters
# Dart: Validate Password
This code snippet shows how to validate a password
**Requirement** :
Password should be more than 8 characters long
It should contain
at least one Uppercase ( Capital ) letter
at least one lowercase character
at least digit and
special character.
void main() {
var p = "ComplextPassword@123";
//will print True if password is complient else false
print(isPasswordCompliant(p));
}
// Password validation function
bool isPasswordCompliant(String password)
{
bool isComplient = false;
bool hasUppercase = false;
bool hasDigits = false;
bool hasLowercase = false;
bool hasSpecialCharacters = false;
var character='';
var i=0;
print(password);
if (! password?.isEmpty) {
// Check if valid special characters are present
hasSpecialCharacters = password.contains(new RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
while (i < password.length){
character = password.substring(i,i+1);
print(character);
if (isDigit(character , 0)){
hasDigits=true;
}else{
if (character == character.toUpperCase()) {
hasUppercase=true;
}
if (character == character.toLowerCase()){
hasLowercase=true;
}
}
i++;
}
}
isComplient = hasDigits & hasUppercase & hasLowercase & hasSpecialCharacters;
return isComplient;
}
bool isDigit(String s, int idx) =>
"0".compareTo(s[idx]) <= 0 && "9".compareTo(s[idx]) >= 0;
@thiagocordeiro
Copy link

thiagocordeiro commented May 9, 2020

bool isPasswordCompliant(String password, [int minLength = 6]) {
  if (password == null || password.isEmpty) {
    return false;
  }

  bool hasUppercase = password.contains(new RegExp(r'[A-Z]'));
  bool hasDigits = password.contains(new RegExp(r'[0-9]'));
  bool hasLowercase = password.contains(new RegExp(r'[a-z]'));
  bool hasSpecialCharacters = password.contains(new RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
  bool hasMinLength = password.length > minLength;

  return hasDigits & hasUppercase & hasLowercase & hasSpecialCharacters & hasMinLength;
}

@MelissaMashiro
Copy link

MelissaMashiro commented Sep 16, 2020

thanks thiago, excelent abreviation

@selcukguvel
Copy link

selcukguvel commented Oct 3, 2020

bool isPasswordCompliant(String password, [int minLength = 6]) {
  if (password == null || password.isEmpty) {
    return false;
  }

  bool hasUppercase = password.contains(new RegExp(r'[A-Z]'));
  bool hasDigits = password.contains(new RegExp(r'[0-9]'));
  bool hasLowercase = password.contains(new RegExp(r'[a-z]'));
  bool hasSpecialCharacters = password.contains(new RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
  bool hasMinLength = password.length > minLength;

  return hasDigits & hasUppercase & hasLowercase & hasSpecialCharacters & hasMinLength;
}

It should be bool hasMinLength = password.length >= minLength;

@apoorvpandey
Copy link

apoorvpandey commented Jan 10, 2021

bool isPasswordCompliant(String password, [int minLength = 6]) {
  if (password == null || password.isEmpty) {
    return false;
  }

  bool hasUppercase = password.contains(new RegExp(r'[A-Z]'));
  bool hasDigits = password.contains(new RegExp(r'[0-9]'));
  bool hasLowercase = password.contains(new RegExp(r'[a-z]'));
  bool hasSpecialCharacters = password.contains(new RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
  bool hasMinLength = password.length > minLength;

  return hasDigits & hasUppercase & hasLowercase & hasSpecialCharacters & hasMinLength;
}

Superb! Thanks a lot

@musheyev
Copy link

musheyev commented Mar 29, 2021

This is great! My 5 cents:

bool isPasswordCompliant(String password, [int minLength = 8]) {
  if (password == null || password.length < minLength) {
    return false;
  }

  bool hasUppercase = password.contains(RegExp(r'[A-Z]'));
  if (hasUppercase) {
    bool hasDigits = password.contains(RegExp(r'[0-9]'));
    if (hasDigits) {
      bool hasLowercase = password.contains(RegExp(r'[a-z]'));
      if (hasLowercase) {
        bool hasSpecialCharacters = password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
        return hasSpecialCharacters;
      }
    }
  }

  return false;
}

@Rakhmen
Copy link

Rakhmen commented Apr 20, 2021

Clear and succinct ;)

  bool isPasswordValid(String password) {
    if (password.length < 8) return false;
    if (!password.contains(RegExp(r"[a-z]"))) return false;
    if (!password.contains(RegExp(r"[A-Z]"))) return false;
    if (!password.contains(RegExp(r"[0-9]"))) return false;
    if (!password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'))) return false;
    return true;
  }

This uses the fail fast pattern which doesn't even waste time trying to compute the longer validations if one of the previous ones is false already ^^

@musheyev
Copy link

musheyev commented Apr 21, 2021

Nice!

@bharathraj-e
Copy link

bharathraj-e commented May 14, 2021

+1

@Rohit7602
Copy link

Rohit7602 commented Sep 30, 2021

main(List args) {
var newtype = NewPassword("Rohit@123");
print(newtype);
}

NewPassword(String Password, [Length = 8]) {
if (Password.length >= Length &&
Password.contains(RegExp('[A-Z]')) &&
Password.contains(RegExp('[a-z]')) &&
Password.contains(RegExp('[0-9]')) &&
Password.contains(RegExp('[!@#%^&*(),.?":{}|<>]'))) {
return true;
} else {
return false;
}
}

@ZaunSupremoXV
Copy link

ZaunSupremoXV commented Feb 28, 2022

validator: (value) {
if (value.lenght > 8){
return "A senha deve ter no mínimo 8 caracteres";
}
if (!RegExp(r'^(?=.?[A-Z])(?=.?[a-z])(?=.?[0-9])(?=.?[!@#$&*~]).{8,}$')
.hasMatch(value)) {
return "A senha deve conter caracteres especias, maiúsculas, minúsculas e números)";
}
return null;
},

@aquadesk
Copy link

aquadesk commented Mar 12, 2022

what about a single quote?

@rizwan992323
Copy link

rizwan992323 commented Jul 12, 2022

good

@SyedMahamudulHasan
Copy link

SyedMahamudulHasan commented Nov 27, 2022

This works as as I expected to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment