Đoạn code kiểm tra độ mạnh password bằng JQuery


Bạn có thể paste đoạn code này vào file html chạy xem sao, sniff hữu ích cho bạn nào validate client lắm đấy!

<script src=”//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js“></script>

<script>
$(document).ready(function(){
$(‘#pass’).keyup(function(e) {
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”);
if (false == enoughRegex.test($(this).val())) {
$(‘#passstrength’).html(‘More Characters’);
} else if (strongRegex.test($(this).val())) {
$(‘#passstrength’).className = ‘ok’;
$(‘#passstrength’).html(‘Strong!’);
} else if (mediumRegex.test($(this).val())) {
$(‘#passstrength’).className = ‘alert’;
$(‘#passstrength’).html(‘Medium!’);
} else {
$(‘#passstrength’).className = ‘error’;
$(‘#passstrength’).html(‘Weak!’);
}
return true;
});
});
</script>
<input type=”password” name=”pass” id=”pass” />
<span id=”passstrength”></span>

Advertisements

Để lại comment đê!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s