Wednesday, 26 December 2018

Javascript: Check password strength using javascript

Here is simple method to check the password strength using javascript, just copy and paste the script given below in your registration field and customize it accordingly.Add this code below inside the body tags of your page.
Some bugs have been fixed and the script is working fine now.
<script language="javascript">
function passwordCheck()
{var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z]) (?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
var pwd = document.getElementById("password");
if (pwd.value.length==0) {
document.getElementById('check').innerHTML = 'Type Password';
} else if (false == enoughRegex.test(pwd.value)) {
document.getElementById('check').innerHTML = 'More Characters';
} else if (strongRegex.test(pwd.value)) {
document.getElementById('check').innerHTML = '<b><span style="color:green">Strong!</span>';
} else if (mediumRegex.test(pwd.value)) {
document.getElementById('check').innerHTML = '</b><b><span style="color:orange">Medium!</span>';
} else {
document.getElementById('check').innerHTML = '</b><b><span style="color:red">Weak!</span>';
}}
</b></script>
<input name="password" id="password" type="text" size="15" maxlength="20" onkeyup="return passwordCheck();" />
<span id="check">Type Password</span>

0 comments:

Post a Comment