Pārbaudiet paroles stiprumu, izmantojot JavaScript vai jQuery un regulārās izteiksmes (arī ar servera puses piemēriem!)
Es veicu dažus pētījumus, lai atrastu labu piemēru paroles stipruma pārbaudītājam, kas izmanto JavaScript un Regulāras izteiksmes (Regulārā izteiksme). Manā darbā esošajā lietojumprogrammā mēs publicējam atpakaļ ziņojumu, lai pārbaudītu paroles stiprumu, un tas ir diezgan neērti mūsu lietotājiem.
Kas ir Regex?
Regulārā izteiksme ir rakstzīmju secība, kas nosaka meklēšanas modeli. Parasti šādus modeļus izmanto virkņu meklēšanas algoritmi atrast or atrast un aizstāt operācijas ar virknēm vai ievades validācijai.
Šis raksts noteikti nav paredzēts, lai iemācītu jums regulāras izteiksmes. Vienkārši ziniet, ka spēja izmantot regulārās izteiksmes absolūti vienkāršos jūsu attīstību, meklējot rakstus tekstā. Ir arī svarīgi atzīmēt, ka lielākā daļa izstrādes valodu ir optimizējušas regulāru izteiksmju lietošanu ... tāpēc, nevis pakāpeniski parsējot un meklējot virknes, Regex parasti ir daudz ātrāks gan servera, gan klienta pusē.
Pirms atradu, es diezgan daudz meklēju tīmeklī piemērs dažas lieliskas regulāras izteiksmes, kas meklē garuma, rakstzīmju un simbolu kombināciju. Tomēr kods manai gaumei bija nedaudz pārmērīgs un pielāgots .NET. Tāpēc es vienkāršoju kodu un ievietoju to JavaScript. Tas ļauj klienta pārlūkprogrammā reāllaikā pārbaudīt paroles stiprumu pirms tās publicēšanas... un sniedz lietotājam dažas atsauksmes par paroles stiprumu.
Ievadiet A paroli
Ar katru tastatūras taustiĦu parole tiek pārbaudīta salīdzinājumā ar parasto izteiksmi, un pēc tam lietotājam tiek sniegta atgriezeniskā saite zem tās.
JavaScript paroles stiprības funkcija
Jūsu darbs IR Klientu apkalpošana Regulāras izteiksmes veiciet fantastisku darbu, samazinot koda garumu. Šī JavaScript funkcija pārbauda paroles stiprumu un to, vai to ir viegli, vidēji, grūti vai ārkārtīgi grūti uzminēt. Kad cilvēks raksta, tajā tiek parādīti padomi, kā mudināt viņu būt stiprākam. Tas apstiprina paroli, pamatojoties uz:
- Garums – Ja garums ir mazāks vai lielāks par 8 rakstzīmēm.
- Jaukts gadījums – Ja parolē ir gan lielie, gan mazie burti.
- Numuri – Ja parolē ir iekļauti cipari.
- Īpašās rakstzīmes – Ja parole ietver speciālās rakstzīmes.
Funkcija parāda grūtības, kā arī dažus padomus paroles turpmākai nostiprināšanai.
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;
}
}
Ja vēlaties atjaunināt uzgaļa krāsu, varat arī to izdarīt, atjauninot kodu pēc // Return results
līnija.
// 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 paroles stiprības funkcija
Izmantojot jQuery, mums faktiski nav jāraksta veidlapa ar ievades atjauninājumu:
<form>
<label for="password">Enter password:</label>
<input type="password" id="password">
<p id="password-strength"></p>
</form>
Ja vēlamies, varam arī mainīt ziņojumu krāsu.
$(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');
}
});
});
Paroles pieprasījuma rūdīšana
Ir svarīgi, lai jūs ne tikai validētu paroles konstrukciju savā JavaScript. Tas ļautu ikvienam, kam ir pārlūkprogrammas izstrādes rīki, apiet skriptu un izmantot jebkuru vēlamo paroli. Jums VIENMĒR jāizmanto servera puses pārbaude, lai apstiprinātu paroles stiprumu pirms tās saglabāšanas savā platformā.
PHP funkcija paroles stiprināšanai
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 funkcija paroles stiprumam
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# funkcija paroles stiprumam
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";
}
}
Java funkcija paroles stiprināšanai
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";
}
}
Un, ja jūs vienkārši meklējat lielisku paroļu ģeneratoru, es tam esmu izveidojis mazu jauku tiešsaistes rīku.