Şifrə gücünü JavaScript və ya jQuery və müntəzəm ifadələrlə yoxlayın (Server tərəfi nümunələri ilə də!)
İstifadə edən bir Parol Gücü yoxlayıcısının yaxşı bir nümunəsini tapmaq üçün araşdırma aparırdım JavaScript və Adi ifadələr (regex). İşimdəki tətbiqdə biz parolun gücünü yoxlamaq üçün bir yazı yazırıq və bu, istifadəçilərimiz üçün olduqca əlverişsizdir.
Regex nədir?
Adi ifadə axtarış modelini təyin edən simvol ardıcıllığıdır. Ümumiyyətlə, bu cür naxışlar üçün simli axtarış alqoritmləri tərəfindən istifadə olunur tapmaq or tapmaq və dəyişdirmək simlər üzərində əməliyyatlar və ya giriş doğrulaması üçün.
Bu yazı mütləq sizə normal ifadələr öyrətmək üçün deyil. Yalnız bilin ki, Müntəzəm İfadələrdən istifadə etmək bacarığınız mətndə naxış axtararkən inkişafınızı tamamilə sadələşdirəcəkdir. Çox inkişaf dillərinin müntəzəm ifadə istifadəsini optimallaşdırdığını da qeyd etmək vacibdir ... beləliklə, strinqləri addım-addım təhlil etmək və axtarmaq əvəzinə, Regex həm server həm də müştəri tərəfində daha sürətli olur.
Tapmadan əvvəl vebdə bir az axtardım nümunə uzunluq, simvol və simvolların birləşməsini axtaran bəzi böyük Normal İfadələr. Bununla belə, kod mənim zövqümə görə bir az həddindən artıq idi və .NET üçün hazırlanmışdır. Beləliklə, kodu sadələşdirdim və JavaScript-də yerləşdirdim. Bu, parolun gücünü geri göndərməzdən əvvəl müştərinin brauzerində real vaxt rejimində təsdiq edir... və parolun gücü ilə bağlı istifadəçiyə bəzi rəylər təqdim edir.
Parol yazın
Klaviaturanın hər vuruşunda parol normal ifadəyə qarşı yoxlanılır və sonra istifadəçiyə altındakı bir müddətdə geribildirim verilir.
JavaScript Şifrə Gücü Funksiyası
The Adi ifadələr kodun uzunluğunu minimuma endirmək üçün fantastik bir iş edin. Bu JavaScript funksiyası parolun gücünü yoxlayır və onu pozmağın asan, orta, çətin və ya çox çətin olduğunu təxmin edir. Şəxs yazdıqca, onu daha güclü olmağa təşviq etmək üçün məsləhətlər göstərir. O, parolu təsdiqləyir:
- Uzunluq – Uzunluq 8 simvoldan az və ya çox olarsa.
- Qarışıq Case – Parolda həm böyük, həm də kiçik hərflər varsa.
- Nömrələr – Parolda nömrələr varsa.
- Xüsusi personajlar – Parolda xüsusi simvollar varsa.
Funksiya çətinliyi, həmçinin parolun daha da sərtləşdirilməsi ilə bağlı bəzi məsləhətləri göstərir.
function checkPasswordStrength(password) {
// Initialize variables
var strength = 0;
var tips = "";
// Check password length
if (password.length < 8) {
tips += "Make the password longer. ";
} else {
strength += 1;
}
// Check for mixed case
if (password.match(/[a-z]/) && password.match(/[A-Z]/)) {
strength += 1;
} else {
tips += "Use both lowercase and uppercase letters. ";
}
// Check for numbers
if (password.match(/\d/)) {
strength += 1;
} else {
tips += "Include at least one number. ";
}
// Check for special characters
if (password.match(/[^a-zA-Z\d]/)) {
strength += 1;
} else {
tips += "Include at least one special character. ";
}
// Return results
if (strength < 2) {
return "Easy to guess. " + tips;
} else if (strength === 2) {
return "Medium difficulty. " + tips;
} else if (strength === 3) {
return "Difficult. " + tips;
} else {
return "Extremely difficult. " + tips;
}
}
Ucun rəngini yeniləmək istəyirsinizsə, kodun sonundan sonra onu yeniləyərək bunu edə bilərsiniz // Return results
xətt.
// Get the paragraph element
var strengthElement = document.getElementById("passwordStrength");
// Return results
if (strength < 2) {
strengthElement.textContent = "Easy to guess. " + tips;
strengthElement.style.color = "red";
} else if (strength === 2) {
strengthElement.textContent = "Medium difficulty. " + tips;
strengthElement.style.color = "orange";
} else if (strength === 3) {
strengthElement.textContent = "Difficult. " + tips;
strengthElement.style.color = "black";
} else {
strengthElement.textContent = "Extremely difficult. " + tips;
strengthElement.style.color = "green";
}
jQuery Şifrə Gücü Funksiyası
jQuery ilə, əslində, oninput yeniləməsi ilə formanı yazmaq məcburiyyətində deyilik:
<form>
<label for="password">Enter password:</label>
<input type="password" id="password">
<p id="password-strength"></p>
</form>
İstəsək, mesajların rəngini də dəyişə bilərik.
$(document).ready(function() {
$('#password').on('input', function() {
var password = $(this).val();
var strength = 0;
var tips = "";
// Check password length
if (password.length < 8) {
tips += "Make the password longer. ";
} else {
strength += 1;
}
// Check for mixed case
if (password.match(/[a-z]/) && password.match(/[A-Z]/)) {
strength += 1;
} else {
tips += "Use both lowercase and uppercase letters. ";
}
// Check for numbers
if (password.match(/\d/)) {
strength += 1;
} else {
tips += "Include at least one number. ";
}
// Check for special characters
if (password.match(/[^a-zA-Z\d]/)) {
strength += 1;
} else {
tips += "Include at least one special character. ";
}
// Update the text and color based on the password strength
var passwordStrengthElement = $('#password-strength');
if (strength < 2) {
passwordStrengthElement.text("Easy to guess. " + tips);
passwordStrengthElement.css('color', 'red');
} else if (strength === 2) {
passwordStrengthElement.text("Medium difficulty. " + tips);
passwordStrengthElement.css('color', 'orange');
} else if (strength === 3) {
passwordStrengthElement.text("Difficult. " + tips);
passwordStrengthElement.css('color', 'black');
} else {
passwordStrengthElement.text("Extremely difficult. " + tips);
passwordStrengthElement.css('color', 'green');
}
});
});
Parol Tələbinizi Sərtləşdirmək
JavaScript daxilində yalnız parol konstruksiyasını təsdiq etməməyiniz vacibdir. Bu, brauzer inkişaf etdirmə alətləri olan hər kəsə skriptdən yan keçməyə və istədikləri paroldan istifadə etməyə imkan verəcəkdir. Şifrə gücünü platformanızda saxlamazdan əvvəl onu yoxlamaq üçün HƏMİŞƏ server tərəfində yoxlamadan istifadə etməlisiniz.
Şifrə Gücü üçün PHP Funksiya
function checkPasswordStrength($password) {
// Initialize variables
$strength = 0;
// Check password length
if (strlen($password) < 8) {
return "Easy to guess";
} else {
$strength += 1;
}
// Check for mixed case
if (preg_match("/[a-z]/", $password) && preg_match("/[A-Z]/", $password)) {
$strength += 1;
}
// Check for numbers
if (preg_match("/\d/", $password)) {
$strength += 1;
}
// Check for special characters
if (preg_match("/[^a-zA-Z\d]/", $password)) {
$strength += 1;
}
// Return strength level
if ($strength < 2) {
return "Easy to guess";
} else if ($strength === 2) {
return "Medium difficulty";
} else if ($strength === 3) {
return "Difficult";
} else {
return "Extremely difficult";
}
}
Parol Gücü üçün Python Funksiya
def check_password_strength(password):
# Initialize variables
strength = 0
# Check password length
if len(password) < 8:
return "Easy to guess"
else:
strength += 1
# Check for mixed case
if any(char.islower() for char in password) and any(char.isupper() for char in password):
strength += 1
# Check for numbers
if any(char.isdigit() for char in password):
strength += 1
# Check for special characters
if any(not char.isalnum() for char in password):
strength += 1
# Return strength level
if strength < 2:
return "Easy to guess"
elif strength == 2:
return "Medium difficulty"
elif strength == 3:
return "Difficult"
else:
return "Extremely difficult"
Şifrə Gücü üçün C# Funksiya
public string CheckPasswordStrength(string password) {
// Initialize variables
int strength = 0;
// Check password length
if (password.Length < 8) {
return "Easy to guess";
} else {
strength += 1;
}
// Check for mixed case
if (password.Any(char.IsLower) && password.Any(char.IsUpper)) {
strength += 1;
}
// Check for numbers
if (password.Any(char.IsDigit)) {
strength += 1;
}
// Check for special characters
if (password.Any(ch => !char.IsLetterOrDigit(ch))) {
strength += 1;
}
// Return strength level
if (strength < 2) {
return "Easy to guess";
} else if (strength == 2) {
return "Medium difficulty";
} else if (strength == 3) {
return "Difficult";
} else {
return "Extremely difficult";
}
}
Şifrə Gücü üçün Java Funksiya
public String checkPasswordStrength(String password) {
// Initialize variables
int strength = 0;
// Check password length
if (password.length() < 8) {
return "Easy to guess";
} else {
strength += 1;
}
// Check for mixed case
if (password.matches(".*[a-z].*") && password.matches(".*[A-Z].*")) {
strength += 1;
}
// Check for numbers
if (password.matches(".*\\d.*")) {
strength += 1;
}
// Check for special characters
if (password.matches(".*[^a-zA-Z\\d].*")) {
strength += 1;
}
// Return strength level
if (strength < 2) {
return "Easy to guess";
} else if (strength == 2) {
return "Medium difficulty";
} else if (strength == 3) {
return "Difficult";
} else {
return "Extremely difficult";
}
}
Əgər sadəcə əla parol generatoru axtarırsınızsa, mən bunun üçün gözəl kiçik onlayn alət hazırlamışam.