Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isUsername from './lib/isUsername';
import toDate from './lib/toDate';
import toFloat from './lib/toFloat';
import toInt from './lib/toInt';
Expand Down Expand Up @@ -246,6 +247,8 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isUsername,

};

export default validator;
5 changes: 5 additions & 0 deletions src/lib/isNumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const numericNoSymbols = /^[0-9]+$/;

export default function isNumeric(str, options) {
assertString(str);

if (str === '' || str.length === 0) {
return false;
}

if (options && options.no_symbols) {
return numericNoSymbols.test(str);
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/isUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assertString from './util/assertString';

export default function isUsername(str) {
assertString(str);
if (str.length < 3 || str.length > 15) {
return false;
}
for (let i = 0; i<str.length; i++) {
let char = str[i];
if (char === '@' || char === '#' || char === '$') {
return false;
}
}
return true;
}
23 changes: 23 additions & 0 deletions test/isUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from 'assert';
import isUsername from '../src/lib/isUsername';

describe('isUsername', () => {
it('should return true for valid usernames', () => {
assert.strictEqual(isUsername('roghana'), true);
assert.strictEqual(isUsername('user123'), true);
});

it('should return false if username length is less than 3', () => {
assert.strictEqual(isUsername('ab'), false);
});

it('should return false if username length is more than 15', () => {
assert.strictEqual(isUsername('verylongusername123'), false);
});

it('should return false if username contains restricted special characters', () => {
assert.strictEqual(isUsername('user@123'), false);
assert.strictEqual(isUsername('hello#'), false);
assert.strictEqual(isUsername('$money'), false);
});
});
Loading