Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created April 14, 2013 10:23
Show Gist options
  • Select an option

  • Save JanPaulEttles/5382227 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/5382227 to your computer and use it in GitHub Desktop.
//validator.js
//This is not a recommended password policy, this is purely an example that allows me to use nodeunit
var minimumLength = 12;
var minimumCriteria = 4; //if you only want to impose 2 of the 4 possible criteria, simply change this to 2
var hasNumeric = /\d+/;
var hasUpper = /[A-Z]+/;
var hasLower = /[a-z]+/;
var hasSpecial = /[!\"£$%^&\*()_\+\-={}\[\];'#:@~,\.\/<>\\?\|]/;
//use the unary plus to convert from true/false to 1/0
//could use the double bitwise not too ~~
exports.validate = function (value) {
if (typeof value !== 'string') {
throw new Error('Expected a string');
}
if (value.length < minimumLength) {
throw new Error('String too short');
}
if (((+hasNumeric.test(value)) +
(+hasUpper.test(value)) +
(+hasLower.test(value)) +
(+hasSpecial.test(value))) < minimumCriteria) {
return false;
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment