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); +};