From e6059086944c27e7d499200738327426b2072f78 Mon Sep 17 00:00:00 2001 From: Flaise Date: Sun, 9 Mar 2014 11:07:52 -0500 Subject: [PATCH] Added alphanumeric validator for convenience. --- lib/validators.js | 3 +++ test/test-validators.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/validators.js b/lib/validators.js index 46e0a40..da83e6c 100644 --- a/lib/validators.js +++ b/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.'); +}; diff --git a/test/test-validators.js b/test/test-validators.js index fa9fdc4..fce0ef8 100644 --- a/test/test-validators.js +++ b/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); +};