Ṣayẹwo Agbara Ọrọigbaniwọle pẹlu JavaScript tabi jQuery ati Awọn ikosile deede (Pẹlu Awọn Apeere-Ẹgbẹ Olupin, paapaa!)

Mo n ṣe iwadi diẹ lori wiwa apẹẹrẹ to dara ti oluyẹwo Agbara Ọrọigbaniwọle ti o nlo JavaScript ati Awọn Ikede deede (regex). Ninu ohun elo ni iṣẹ mi, a ṣe ifiweranṣẹ pada lati rii daju agbara ọrọ igbaniwọle ati pe ko rọrun fun awọn olumulo wa.
Kini Regex?
Ifihan deede jẹ ọkọọkan awọn ohun kikọ ti o ṣalaye apẹrẹ wiwa kan. Nigbagbogbo, iru awọn apẹẹrẹ lo nipasẹ awọn alugoridimu wiwa okun fun ri or wa ki o rọpo awọn iṣẹ lori awọn okun, tabi fun afọwọsi igbewọle.
Nkan yii ko daju lati kọ ọ awọn ifihan deede. O kan mọ pe agbara lati lo Awọn Ifihan deede yoo ṣe irọrun idagbasoke rẹ ni pipe bi o ṣe wa awọn ilana ninu ọrọ. O tun ṣe pataki lati ṣe akiyesi pe ọpọlọpọ awọn ede idagbasoke ti ni iṣapeye lilo ikosile deede… nitorinaa dipo itupalẹ ati wiwa awọn okun ni igbesẹ-ni-igbesẹ, Regex jẹ iyara pupọ mejeeji olupin ati ẹgbẹ alabara.
Mo wa oju opo wẹẹbu diẹ ṣaaju ki Mo to rii apẹẹrẹ ti diẹ ninu awọn nla Deede Expressions ti o nwa fun a apapo ti ipari, ohun kikọ, ati awọn aami. Sibẹsibẹ, awọn koodu je kekere kan nmu fun mi lenu ati sile fun .NET. Nitorina ni mo ṣe rọrun koodu ati fi sii ni JavaScript. Eyi jẹ ki o fọwọsi agbara ọrọ igbaniwọle ni akoko gidi lori ẹrọ aṣawakiri alabara ṣaaju fifiranṣẹ pada… ati pese awọn esi diẹ si olumulo lori agbara ọrọ igbaniwọle.
Tẹ Ọrọigbaniwọle kan
Pẹlu ọpọlọ kọọkan ti bọtini itẹwe, a ti ni igbaniwọle ọrọigbaniwọle lodi si ikosile deede ati lẹhinna a ti pese esi si olumulo ni igba kan labẹ rẹ.
JavaScript Ọrọigbaniwọle Agbara Išė
awọn Awọn Ikede deede ṣe kan ikọja ise ti dindinku awọn ipari ti awọn koodu. Iṣẹ JavaScript yii n ṣayẹwo agbara ọrọ igbaniwọle kan ati boya fifọ o rọrun, alabọde, nira, tabi nira pupọ lati gboju. Bi eniyan ṣe n tẹ, o ṣafihan awọn imọran lori iyanju lati ni okun sii. O fọwọsi ọrọ igbaniwọle ti o da lori:
- ipari - Ti ipari ba wa labẹ tabi ju awọn ohun kikọ 8 lọ.
- Adalu Case - Ti ọrọ igbaniwọle ba ni awọn ohun kikọ oke ati isalẹ.
- Awọn nọmba – Ti ọrọ igbaniwọle ba pẹlu awọn nọmba.
- Awọn lẹta Pataki - Ti ọrọ igbaniwọle ba pẹlu awọn ohun kikọ pataki.
Iṣẹ naa ṣafihan iṣoro naa daradara bi awọn imọran diẹ lori lile ọrọ igbaniwọle siwaju.
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;
}
} Ti o ba fẹ ṣe imudojuiwọn awọ ti imọran, o le ṣe bẹ daradara nipa mimu koodu dojuiwọn lẹhin ti // Return results laini.
// 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 Ọrọigbaniwọle Agbara Išė
Pẹlu jQuery, a ko ni lati kọ fọọmu gangan pẹlu imudojuiwọn oninput:
<form>
<label for="password">Enter password:</label>
<input type="password" id="password">
<p id="password-strength"></p>
</form> A tun le yipada awọ ti awọn ifiranṣẹ ti a ba fẹ.
$(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');
}
});
}); Ikunkun ibeere Ọrọigbaniwọle rẹ
O ṣe pataki pe o ko kan fọwọsi ikole ọrọ igbaniwọle laarin JavaScript rẹ. Eyi yoo jẹ ki ẹnikẹni ti o ni awọn irinṣẹ idagbasoke aṣawakiri lati fori iwe afọwọkọ naa ki o lo ọrọ igbaniwọle eyikeyi ti wọn fẹ. O yẹ ki o nigbagbogbo lo ayẹwo-ẹgbẹ olupin lati jẹrisi agbara ọrọ igbaniwọle ṣaaju ki o to tọju rẹ si pẹpẹ rẹ.
Iṣẹ PHP Fun Agbara Ọrọigbaniwọle
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";
}
}
Python Iṣẹ Fun Agbara Ọrọigbaniwọle
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"
C # Iṣẹ Fun Agbara Ọrọigbaniwọle
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";
}
}
Iṣẹ Java Fun Agbara Ọrọigbaniwọle
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";
}
}
Ati pe ti o ba n wa olupilẹṣẹ ọrọ igbaniwọle nla kan, Mo ti kọ ohun elo ori ayelujara ti o wuyi fun iyẹn.



