Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ Validator | Description
**equals(str, comparison)** | check if the string matches the comparison.
**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque.
**isAfter(str [, options])** | check if the string is a date that is after the specified date.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now).
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAscii(str)** | check if the string contains ASCII chars only.
**isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.<br/> When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32].
**isBase58(str)** | check if the string is base58 encoded.
Expand Down
25 changes: 22 additions & 3 deletions src/lib/alpha.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export const alpha = {
eo: /^[ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i,
'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i,
'si-LK': /^[\u0D80-\u0DFF]+$/,
'ta-IN': /^[\u0B80-\u0BFF]+$/i,
'te-IN': /^[\u0C00-\u0C7F]+$/i,
'kn-IN': /^[\u0C80-\u0CFF]+$/i,
'ml-IN': /^[\u0D00-\u0D7F]+$/i,
'gu-IN': /^[\u0A80-\u0AFF]+$/i,
'pa-IN': /^[\u0A00-\u0A7F]+$/i,
'or-IN': /^[\u0B00-\u0B7F]+$/i,
'bn-IN': /^[\u0980-\u09FF]+$/i,
};

export const alphanumeric = {
Expand Down Expand Up @@ -75,10 +83,18 @@ export const alphanumeric = {
ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/,
he: /^[0-9א-ת]+$/,
fa: /^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$/i,
bn: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ০১২৩৪৫৬৭৮৯ৰৱ৲৳৴৵৶৷৸৹৺৻']+$/,
bn: /^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ০১২৩৪৫৬৭৮৯ৰৱ৲৳৴৵৶৷৸৹৺৻.']+$/,
eo: /^[0-9ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i,
'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i,
'si-LK': /^[0-9\u0D80-\u0DFF]+$/,
'ta-IN': /^[0-9\u0B80-\u0BFF.]+$/i,
'te-IN': /^[0-9\u0C00-\u0C7F.]+$/i,
'kn-IN': /^[0-9\u0C80-\u0CFF.]+$/i,
'ml-IN': /^[0-9\u0D00-\u0D7F.]+$/i,
'gu-IN': /^[0-9\u0A80-\u0AFF.]+$/i,
'pa-IN': /^[0-9\u0A00-\u0A7F.]+$/i,
'or-IN': /^[0-9\u0B00-\u0B7F.]+$/i,
'bn-IN': /^[0-9\u0980-\u09FF.]+$/i,
};

export const decimal = {
Expand Down Expand Up @@ -126,12 +142,15 @@ for (let locale, i = 0; i < bengaliLocales.length; i++) {

// Source: https://en.wikipedia.org/wiki/Decimal_mark
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
// sorted
export const commaDecimal = [
'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'eo', 'es-ES', 'fr-CA', 'fr-FR',
'id-ID', 'it-IT', 'ku-IQ', 'hi-IN', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT',
'ru-RU', 'kk-KZ', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN',
'gu-IN', 'hi-IN', 'hu-HU', 'id-ID', 'it-IT', 'kk-KZ', 'kn-IN', 'ku-IQ', 'ml-IN', 'nb-NO',
'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sr-RS',
'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'tr-TR', 'uk-UA', 'vi-VN',
];


for (let i = 0; i < dotDecimal.length; i++) {
decimal[dotDecimal[i]] = decimal['en-US'];
}
Expand Down
254 changes: 252 additions & 2 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,100 @@ describe('Validators', () => {
});
});

it('should validate Tamil alpha strings', () => {
test({
validator: 'isAlpha',
args: ['ta-IN'],
valid: [
'அஆஇஈஉஊஎஏஐஒஓஔகஙசஞடணதநபமயரலவழளறனஶஜஷஸஹ',
'தமிழ்',
],
invalid: [
'தமிழ்123',
'தமிழ் ',
'தமிழ்.',
'abc',
'',
],
});
});
it('should validate Telugu alpha strings', () => {
test({
validator: 'isAlpha',
args: ['te-IN'],
valid: [
'అఆఇఈఉఊఋఌఎఏఐఒఓఔకఖగఘఙచఛజఝఞటఠడఢణతథదధనపఫబభమయరలవశషసహ',
'తెలుగు',
],
invalid: ['తెలుగు123', 'తెలుగు.', 'abc', ''],
});
});
it('should validate Kannada alpha strings', () => {
test({
validator: 'isAlpha',
args: ['kn-IN'],
valid: [
'ಅಆಇಈಉಊಋಎಏಐಒಓಔಕಖಗಘಙಚಛಜಝಞಟಠಡಢಣತಥದಧನಪಫಬಭಮಯರಲವಶಷಸಹಳ',
'ಕನ್ನಡ',
],
invalid: ['ಕನ್ನಡ123', 'ಕನ್ನಡ.', 'abc', ''],
});
});
it('should validate Malayalam alpha strings', () => {
test({
validator: 'isAlpha',
args: ['ml-IN'],
valid: [
'അആഇഈഉഊഋഎഏഐഒഓഔകഖഗഘങചഛജഝഞടഠഡഢണതഥദധനപഫബഭമയരലവശഷസഹള',
'മലയാളം',
],
invalid: ['മലയാളം123', 'മലയാളം.', 'abc', ''],
});
});
it('should validate Gujarati alpha strings', () => {
test({
validator: 'isAlpha',
args: ['gu-IN'],
valid: [
'અઆઇઈઉઊઋએઐઓઔકખગઘચછજઝટઠડઢણતથદધનપફબભમયરલવશષસહળ',
'ગુજરાતી',
],
invalid: ['ગુજરાતી123', 'ગુજરાતી.', 'abc', ''],
});
});
it('should validate Punjabi alpha strings', () => {
test({
validator: 'isAlpha',
args: ['pa-IN'],
valid: [
'ਅਆਇਈਉਊਏਐਓਔਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵਸ਼ਸਹ',
'ਪੰਜਾਬੀ',
],
invalid: ['ਪੰਜਾਬੀ123', 'ਪੰਜਾਬੀ.', 'abc', ''],
});
});
it('should validate Odia alpha strings', () => {
test({
validator: 'isAlpha',
args: ['or-IN'],
valid: [
'ଅଆଇଈଉଊଋଌଏଐଓଔକଖଗଘଙଚଛଜଝଞଟଠଡଢଣତଥଦଧନପଫବଭମଯରଲଶଷସହଳ',
'ଓଡ଼ିଆ',
],
invalid: ['ଓଡ଼ିଆ123', 'ଓଡ଼ିଆ.', 'abc', ''],
});
});
it('should validate Bengali alpha strings', () => {
test({
validator: 'isAlpha',
args: ['bn-IN'],
valid: [
'অআইঈউঊঋএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ',
'বাংলা',
],
invalid: ['বাংলা123', 'বাংলা.', 'abc', ''],
});
});
it('should validate persian alpha strings', () => {
test({
validator: 'isAlpha',
Expand All @@ -1918,7 +2012,6 @@ describe('Validators', () => {
],
});
});

it('should validate Thai alpha strings', () => {
test({
validator: 'isAlpha',
Expand All @@ -1934,7 +2027,6 @@ describe('Validators', () => {
],
});
});

it('should validate Korea alpha strings', () => {
test({
validator: 'isAlpha',
Expand Down Expand Up @@ -2699,6 +2791,164 @@ describe('Validators', () => {
],
});
});
it('should validate Tamil alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['ta-IN'],
valid: [
'தமிழ்',
'தமிழ்123',
'அஆஇஈ123',
'தமிழ்123.45',
'123.45',
'தமிழ்.',
],
invalid: [
'தமிழ் ',
'abc',
'',
],
});
});

it('should validate Telugu alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['te-IN'],
valid: [
'తెలుగు',
'తెలుగు123',
'అఆఇఈ123',
'తెలుగు123.45',
'123.45',
'తెలుగు.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Kannada alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['kn-IN'],
valid: [
'ಕನ್ನಡ',
'ಕನ್ನಡ123',
'ಅಆಇಈ123',
'ಕನ್ನಡ123.45',
'123.45',
'ಕನ್ನಡ.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Malayalam alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['ml-IN'],
valid: [
'മലയാളം',
'മലയാളം123',
'അആഇഈ123',
'മലയാളം123.45',
'123.45',
'മലയാളം.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Gujarati alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['gu-IN'],
valid: [
'ગુજરાતી',
'ગુજરાતી123',
'અઆઇઈ123',
'ગુજરાતી123.45',
'123.45',
'ગુજરાતી.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Punjabi alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['pa-IN'],
valid: [
'ਪੰਜਾਬੀ',
'ਪੰਜਾਬੀ123',
'ਅਆਇਈ123',
'ਪੰਜਾਬੀ123.45',
'123.45',
'ਪੰਜਾਬੀ.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Odia alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['or-IN'],
valid: [
'ଓଡ଼ିଆ',
'ଓଡ଼ିଆ123',
'ଅଆଇଈ123',
'ଓଡ଼ିଆ123.45',
'123.45',
'ଓଡ଼ିଆ.',
],
invalid: [
'abc',
'',
],
});
});

it('should validate Bengali alphanumeric strings', () => {
test({
validator: 'isAlphanumeric',
args: ['bn-IN'],
valid: [
'বাংলা',
'বাংলা১২৩',
'অআইঈ১২৩',
'১২৩৪৫৬৭৮৯০',
'বাংলা.১২৩',
'১২৩.৪৫',
'বাংলা.',
],
invalid: [
'abc',
'বাংলা123',
'123',
'বাংলা ১২৩',
'বাংলা,১২৩',
'১২৩٫৪৫',
'',
],
});
});

it('should error on invalid locale', () => {
test({
Expand Down