Skip to content

Commit e7698f7

Browse files
committed
Merge pull request #462 from eserozvataf/master
Added isWhitelisted validator.
2 parents f52af1d + ed94c42 commit e7698f7

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ $ bower install validator-js
7575
- **isUUID(str [, version])** - check if the string is a UUID (version 3, 4 or 5).
7676
- **isUppercase(str)** - check if the string is uppercase.
7777
- **isVariableWidth(str)** - check if the string contains a mixture of full and half-width chars.
78+
- **isWhitelisted(str, chars)** - checks characters if they appear in the whitelist.
7879
- **matches(str, pattern [, modifiers])** - check if string matches the pattern. Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`.
7980

8081
### Sanitizers

test/validators.js

+5
Original file line numberDiff line numberDiff line change
@@ -2402,4 +2402,9 @@ describe('Validators', function () {
24022402
]
24032403
});
24042404
});
2405+
2406+
it('should validate whitelisted characters', function () {
2407+
test({ validator: 'isWhitelisted', args: ['abcdefghijklmnopqrstuvwxyz-'], valid: ['foo', 'foobar', 'baz-foo'],
2408+
invalid: ['foo bar', 'fo.bar', 'türkçe'] });
2409+
});
24052410
});

validator.js

+10
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,16 @@
582582
return false;
583583
};
584584

585+
validator.isWhitelisted = function (str, chars) {
586+
for (var i = str.length - 1; i >= 0; i--) {
587+
if (chars.indexOf(str[i]) === -1) {
588+
return false;
589+
}
590+
}
591+
592+
return true;
593+
};
594+
585595
validator.isCreditCard = function (str) {
586596
var sanitized = str.replace(/[^0-9]+/g, '');
587597
if (!creditCard.test(sanitized)) {

0 commit comments

Comments
 (0)