I want to create some kind of password checker with regex and update progress bar depending on password strength.
So I created array of objects because I need to check regex and if input value matches regex then pass percentages to the progress bar.
var passwordsRegex = [{ weak: '^[a-z]+$', percentage: 20},
{ good: '[a-zA-Z]+$', percentage: 50},
{ normal: '^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$', percentage: 70},
{ strong: '(?=^.{8,}$)(?=.*\d)(?=.*[!@#$%^&*]+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$', percentage: 100}];
$('#pass').on('keyup', function(e) {
var value = $(this).val();
if(value == passwordsRegex[?].?) {
...
}
});
function progress(percent, $element) {
var progressBarWidth = percent * $element.width() / 100;
$element.find('div').animate({ width: progressBarWidth }, 500).html(percent + "% ");
}
progress(50, $('#progressBar'))
Can someone help how to check if value matches with one of the regex without writing a lot of conditional statements.
Thanks