Skip to content

Instantly share code, notes, and snippets.

@rahulbagal
Created April 26, 2019 11:53
Show Gist options
  • Save rahulbagal/4a06a997497e6f921663b69e5286d859 to your computer and use it in GitHub Desktop.
Save rahulbagal/4a06a997497e6f921663b69e5286d859 to your computer and use it in GitHub Desktop.
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;
@Johnvinith
Copy link

Johnvinith commented Jan 31, 2023

RegExp(r"^(?=.[a-z])(?=.[A-Z])(?=.*\d)[a-zA-Z\d\w\W]{8,}$")

all in one

min 8
uppercase
lowercase
special charecter
number

code can be

if(password.containes(RegExp(r"^(?=.[a-z])(?=.[A-Z])(?=.*\d)[a-zA-Z\d\w\W]{8,}$")))
{
// valid
} else
{
// not valid
}

@rahulbagal
Copy link
Author

I asked ChatGPT to do this again

Question:
Write a c# code to validate password with the following requirements
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.

Here is what chatGPT answered

Here is a sample code to validate password in C# using a regular expression pattern:

using System;
using System.Text.RegularExpressions;

namespace PasswordValidation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(IsPasswordValid("Password123#"));
            Console.WriteLine(IsPasswordValid("password123#"));
            Console.WriteLine(IsPasswordValid("PASSWORD123#"));
            Console.WriteLine(IsPasswordValid("Password123"));
            Console.WriteLine(IsPasswordValid("password123"));
        }

        static bool IsPasswordValid(string password)
        {
            string pattern = @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^\w\d\s]).{8,}$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(password);
        }
    }
}

This code uses a regular expression pattern ^(?=.[A-Z])(?=.[a-z])(?=.\d)(?=.[^\w\d\s]).{8,}$ to check for the required conditions:

(?=.[A-Z]) checks for at least one uppercase letter
(?=.
[a-z]) checks for at least one lowercase letter
(?=.\d) checks for at least one digit
(?=.
[^\w\d\s]) checks for at least one special character
.{8,} checks for a minimum length of 8 characters.
The function IsPasswordValid uses the Regex.IsMatch method to check if the password string matches the pattern.

@debojyoti452
Copy link

extension method:

extension ValidatorExt on String {
  // isEmail validator
  bool get isEmail {
    final emailRegex = RegExp(
        r'^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+');
    return emailRegex.hasMatch(this);
  }

  // password validator for 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 special character
  bool get isPasswordCompliant {
    String password = this;
    bool hasUppercase = password.contains(RegExp(r'[A-Z]'));
    bool hasLowercase = password.contains(RegExp(r'[a-z]'));
    bool hasDigits = password.contains(RegExp(r'[0-9]'));
    bool hasSpecialCharacters = password.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
    bool hasMinLength = password.length > 8;

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

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