Skip to content

Commit 2fc01d8

Browse files
committed
Add isTwitterHandle validator
- Add new validator for Twitter handles (@username format) - Accepts handles with or without @ symbol - 1-15 characters, letters, numbers, and underscores only - Add comprehensive test cases covering valid/invalid scenarios - Update README.md documentation - Export validator in main index Fixes validation for Twitter usernames/handles for Hacktoberfest contribution.
1 parent 6f436be commit 2fc01d8

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Validator | Description
167167
**isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
168168
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default', 'withSeconds', withOptionalSeconds`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format, `'withOptionalSeconds'` will validate `'HH:MM'` and `'HH:MM:SS'` formats.
169169
**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`.<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE', 'uk-UA']`.
170+
**isTwitterHandle(str)** | check if the string is a valid Twitter handle (username). Accepts handles with or without the `@` symbol. Handle must be 1-15 characters long and can contain letters, numbers, and underscores only.
170171
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`protocols` - valid protocols can be modified with this option.<br/>`require_tld` - If set to false isURL will not check if the URL's host includes a top-level domain.<br/>`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.<br/>`require_host` - if set to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.<br/>`allow_underscores` - if set to true, the validator will allow underscores in the URL.<br/>`host_whitelist` - if set to an array of strings or regexp, and the domain matches none of the strings defined in it, the validation fails.<br/>`host_blacklist` - if set to an array of strings or regexp, and the domain matches any of the strings defined in it, the validation fails.<br/>`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`disallow_auth` - if set to true, the validator will fail if the URL contains an authentication component, e.g. `http://username:password@example.com`.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set, isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).<br/>
171172
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).
172173
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, `'all'` or `'loose'`. The `'loose'` option checks if the string is a UUID-like string with hexadecimal values, ignoring RFC9565.

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import normalizeEmail from './lib/normalizeEmail';
127127
import isSlug from './lib/isSlug';
128128
import isLicensePlate from './lib/isLicensePlate';
129129
import isStrongPassword from './lib/isStrongPassword';
130+
import isTwitterHandle from './lib/isTwitterHandle';
130131

131132
import isVAT from './lib/isVAT';
132133

@@ -240,6 +241,7 @@ const validator = {
240241
isSlug,
241242
isStrongPassword,
242243
isTaxID,
244+
isTwitterHandle,
243245
isDate,
244246
isTime,
245247
isLicensePlate,
@@ -248,3 +250,4 @@ const validator = {
248250
};
249251

250252
export default validator;
253+
export { default as isTwitterHandle } from './lib/isTwitterHandle';

src/lib/isTwitterHandle.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import assertString from './util/assertString';
2+
3+
/* eslint-disable no-control-regex */
4+
const twitterHandle = /^@?[A-Za-z0-9_]{1,15}$/;
5+
/* eslint-enable no-control-regex */
6+
7+
export default function isTwitterHandle(str) {
8+
assertString(str);
9+
return twitterHandle.test(str);
10+
}

test/validators.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15911,4 +15911,49 @@ describe('Validators', () => {
1591115911
],
1591215912
});
1591315913
});
15914+
it('should validate Twitter handles', () => {
15915+
test({
15916+
validator: 'isTwitterHandle',
15917+
valid: [
15918+
'@twitter',
15919+
'@github',
15920+
'@validatorjs',
15921+
'@user123',
15922+
'@test_user',
15923+
'@a',
15924+
'@123456789012345', // 15 characters
15925+
'twitter', // without @ symbol
15926+
'github',
15927+
'validatorjs',
15928+
'user123',
15929+
'test_user',
15930+
'a',
15931+
'123456789012345',
15932+
'a_b_c_d_e_f_g_h',
15933+
'USER123', // case insensitive
15934+
'Test_User',
15935+
],
15936+
invalid: [
15937+
'@1234567890123456', // 16 characters (too long)
15938+
'@user-name', // hyphen not allowed
15939+
'@user name', // space not allowed
15940+
'@user.name', // dot not allowed
15941+
'@user@name', // @ not allowed in middle
15942+
'@user+name', // plus not allowed
15943+
'@user#name', // hash not allowed
15944+
'@user$name', // dollar not allowed
15945+
'@', // empty handle
15946+
'', // empty string
15947+
'@@twitter', // double @
15948+
'@user!', // exclamation not allowed
15949+
'@user?', // question mark not allowed
15950+
'@user*', // asterisk not allowed
15951+
'@user%', // percent not allowed
15952+
'user-name', // hyphen not allowed even without @
15953+
'user name', // space not allowed even without @
15954+
'user.name', // dot not allowed even without @
15955+
'1234567890123456', // 16 characters (too long) without @
15956+
],
15957+
});
15958+
});
1591415959
});

0 commit comments

Comments
 (0)