Dart : How to check if password contains all required characters · GitHub
source link: https://gist.github.com/rahulbagal/4a06a997497e6f921663b69e5286d859
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to check if password contains all required characters · GitHub
Instantly share code, notes, and snippets.
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;
}
thanks thiago, excelent abreviation
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;
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
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;
}
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 ^^
Nice!
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;
}
}
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;
},
what about a single quote?
This works as as I expected to.
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
}
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK