Skip to content

Commit

Permalink
Added alphanumeric validator for convenience.
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaise committed Mar 9, 2014
1 parent 44c67ad commit e605908
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 e605908

Please sign in to comment.