Skip to content

Commit

Permalink
Merge pull request #98 from Flaise/master
Browse files Browse the repository at this point in the history
Added alphanumeric validator for convenience.
  • Loading branch information
ljharb committed Mar 9, 2014
2 parents 44c67ad + e605908 commit 69b24f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,6 @@ exports.date = function (message) {
};
};

exports.alphanumeric = function(message) {
return exports.regexp(/^[a-zA-Z0-9]*$/, message || 'Letters and numbers only.');
};
20 changes: 20 additions & 0 deletions test/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 69b24f9

Please sign in to comment.