JavaScript yoki jQuery va oddiy iboralar yordamida parol kuchini tekshiring (server tomonidagi misollar bilan ham!)
Men foydalanadigan Parolni kuchaytirish tekshirgichining yaxshi namunasini topish bo'yicha tadqiqotlar olib bordim JavaScript va Muntazam iboralar (regex). Mening ishimdagi ilovada biz parol kuchini tekshirish uchun post qilamiz va bu bizning foydalanuvchilarimiz uchun juda noqulay.
Regex nima?
Doimiy ifoda - bu izlash uslubini belgilaydigan belgilar ketma-ketligi. Odatda, bunday naqshlar uchun qatorlarni qidirish algoritmlari ishlatiladi topish or toping va almashtiring satrlarda yoki kiritishni tekshirish uchun operatsiyalar.
Ushbu maqola sizga doimiy so'z birikmalarini o'rgatishga qodir emas. Shuni bilingki, odatiy iboralardan foydalanish qobiliyati sizning matningizdagi naqshlarni qidirishda rivojlanishingizni mutlaqo soddalashtiradi. Shuni ham ta'kidlash kerakki, ko'pgina ishlab chiqish tillari muntazam ravishda ekspression foydalanishni optimallashtirdi ... shuning uchun satrlarni bosqichma-bosqich tahlil qilish va qidirishdan ko'ra, Regex odatda serverda ham, mijoz tomonidan ham tezroq ishlaydi.
Topishdan oldin veb-saytni biroz qidirdim bir misol uzunlik, belgilar va belgilar kombinatsiyasini qidiradigan ba'zi ajoyib Muntazam iboralar. Biroq, kod mening didim uchun biroz ortiqcha edi va .NET uchun mo'ljallangan. Shunday qilib, men kodni soddalashtirdim va uni JavaScript-ga qo'ydim. Bu uni qayta joylashtirishdan oldin mijozning brauzerida real vaqt rejimida parol kuchini tekshirishga imkon beradi… va foydalanuvchiga parolning kuchi haqida ba'zi fikr bildiradi.
Parolni kiriting
Klaviaturaning har bir zarbasi bilan parol odatdagi ifodaga nisbatan sinovdan o'tkaziladi va keyin uning ostidagi foydalanuvchiga fikr bildiriladi.
JavaScript parolni mustahkamlash funktsiyasi
The Muntazam iboralar kod uzunligini minimallashtirish uchun ajoyib ishni bajaring. Ushbu JavaScript funksiyasi parolning mustahkamligini va uni aniqlash oson, o'rtacha, qiyin yoki juda qiyin ekanligini tekshiradi. Shaxs yozganda, u kuchliroq bo'lishga undash bo'yicha maslahatlarni ko'rsatadi. U parolni quyidagilarga asoslanib tasdiqlaydi:
- uzunlik – Agar uzunlik 8 ta belgidan kam yoki undan ortiq boʻlsa.
- Aralash kassa – Parolda katta va kichik harflar bo‘lsa.
- raqamlari – Agar parolda raqamlar boʻlsa.
- Maxsus belgilar – Agar parolda maxsus belgilar boʻlsa.
Funktsiya qiyinchilikni, shuningdek, parolni yanada qattiqlashtirish bo'yicha ba'zi maslahatlarni ko'rsatadi.
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;
}
}
Agar siz uchi rangini yangilamoqchi bo'lsangiz, kodni keyinroq yangilash orqali ham buni amalga oshirishingiz mumkin // Return results
liniyasi.
// 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 parolni mustahkamlash funktsiyasi
JQuery bilan biz formani oninput yangilanishi bilan yozishimiz shart emas:
<form>
<label for="password">Enter password:</label>
<input type="password" id="password">
<p id="password-strength"></p>
</form>
Agar xohlasak, xabarlarning rangini ham o'zgartirishimiz mumkin.
$(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 so'rovini qattiqlashtirish
Siz faqat JavaScript-da parol tuzilishini tasdiqlamasligingiz muhim. Bu brauzerni ishlab chiqish vositalariga ega bo'lgan har kimga skriptni chetlab o'tish va xohlagan parolidan foydalanish imkonini beradi. Parol kuchini platformangizda saqlashdan oldin uni tekshirish uchun DOIMO server tomonidagi tekshiruvdan foydalanishingiz kerak.
Parol mustahkamligi uchun PHP funksiyasi
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 mustahkamligi uchun Python funktsiyasi
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"
Parol mustahkamligi uchun C# funktsiyasi
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";
}
}
Parol mustahkamligi uchun Java funktsiyasi
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";
}
}
Va agar siz shunchaki ajoyib parol ishlab chiqaruvchisini qidirayotgan bo'lsangiz, men buning uchun ajoyib onlayn vositani yaratdim.