Regex demo

by Cristian González

JavaScript

// The FE will check the password:
// has at least 8 alphanumeric characters      (REGEX 1)
// with at least 2 numbers, letters and a special character
// or has at least 12 alphanumeric characters      (REGEX 2)
// with at least 2 numbers and letters (no special character required)

var pwdList = [
    'abcdabc1',
    'abcdab11',
    'abcda*11',
    'ABCDA*11',
    'abcdabc1abcd',
    'abcdab11abcd',
    'abcdab11abcd',
    'ABCDAB11ABCD'
  ],
  re1 = /^(?=.{8,11}$)(?=.*[a-zA-Z])(?=(?:\D*\d){2})(?=.*[#?!@$%^&*-]).*$/,
	re2 = /^(?=.{12,72}$)(?=.*[a-zA-Z])(?=(?:\D*\d){2}).*$/;
  
	document.write('<span style="color:white">Regex 1</span><br/>');
	
  pwdList.forEach(function (pw) {
    document.write('<span style="color:'+ (re1.test(pw) ? 'green':'red') + '">' + pw + '</span><br/>');
  });
	
	document.write('<span style="color:white">Regex 2</span><br/>');

  pwdList.forEach(function (pw) {
    document.write('<span style="color:'+ (re2.test(pw) ? 'green':'red') + '">' + pw + '</span><br/>');
  });