Skip to content
Browse files

Added alphanumeric validator for convenience.

  • Loading branch information...
1 parent 44c67ad commit e6059086944c27e7d499200738327426b2072f78 @Flaise Flaise committed Mar 9, 2014
Showing with 23 additions and 0 deletions.
  1. +3 −0 lib/validators.js
  2. +20 −0 test/test-validators.js
View
3 lib/validators.js
@@ -175,3 +175,6 @@ exports.date = function (message) {
};
};
+exports.alphanumeric = function(message) {
+ return exports.regexp(/^[a-zA-Z0-9]*$/, message || 'Letters and numbers only.');
+};
View
20 test/test-validators.js
@@ -247,3 +247,23 @@ exports.color = function (test) {
async.parallel(tests, test.done);
};
+exports.alphanumeric = function (test) {
+ function makeTest(message, data, expected) {
+ return function (callback) {
+ validators.alphanumeric(message)('form', {data: data}, function(err) {
+ test.equals(err, expected);
+ callback();
+ });
+ };
+ }
+
+ var tests = [
+ makeTest(undefined, 'asdf', undefined),
+ makeTest(undefined, '278', undefined),
+ makeTest(undefined, '%', 'Letters and numbers only.'),
+ makeTest(' qwer', 'a a', ' qwer'),
+ makeTest('_r ', ' 1 ', '_r ')
+ ];
+
+ async.parallel(tests, test.done);
+};

0 comments on commit e605908

Please sign in to comment.
Something went wrong with that request. Please try again.