Skip to content

Commit

Permalink
Merge pull request #462 from eserozvataf/master
Browse files Browse the repository at this point in the history
Added isWhitelisted validator.
  • Loading branch information
chriso committed Dec 3, 2015
2 parents f52af1d + ed94c42 commit e7698f7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ $ bower install validator-js
- **isUUID(str [, version])** - check if the string is a UUID (version 3, 4 or 5).
- **isUppercase(str)** - check if the string is uppercase.
- **isVariableWidth(str)** - check if the string contains a mixture of full and half-width chars.
- **isWhitelisted(str, chars)** - checks characters if they appear in the whitelist.
- **matches(str, pattern [, modifiers])** - check if string matches the pattern. Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`.

### Sanitizers
Expand Down
5 changes: 5 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2402,4 +2402,9 @@ describe('Validators', function () {
]
});
});

it('should validate whitelisted characters', function () {
test({ validator: 'isWhitelisted', args: ['abcdefghijklmnopqrstuvwxyz-'], valid: ['foo', 'foobar', 'baz-foo'],
invalid: ['foo bar', 'fo.bar', 'türkçe'] });
});
});
10 changes: 10 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@
return false;
};

validator.isWhitelisted = function (str, chars) {
for (var i = str.length - 1; i >= 0; i--) {
if (chars.indexOf(str[i]) === -1) {
return false;
}
}

return true;
};

validator.isCreditCard = function (str) {
var sanitized = str.replace(/[^0-9]+/g, '');
if (!creditCard.test(sanitized)) {
Expand Down
Loading

0 comments on commit e7698f7

Please sign in to comment.