Created
April 14, 2013 10:23
-
-
Save JanPaulEttles/5382227 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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