Skip to content

Commit abcc8ec

Browse files
feat(isAlpha, isAlphanumeric): add support for Indic locales (ta-IN, te-IN, kn-IN, ml-IN, gu-IN, pa-IN, or-IN) (#2576)
1 parent 72573b3 commit abcc8ec

File tree

3 files changed

+271
-4
lines changed

3 files changed

+271
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ Validator | Description
8888
**equals(str, comparison)** | check if the string matches the comparison.
8989
**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque.
9090
**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).
91-
**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.
92-
**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.
91+
**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.
92+
**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.
9393
**isAscii(str)** | check if the string contains ASCII chars only.
9494
**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].
9595
**isBase58(str)** | check if the string is base58 encoded.

src/lib/alpha.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ export const alpha = {
3838
eo: /^[ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i,
3939
'hi-IN': /^[\u0900-\u0961]+[\u0972-\u097F]*$/i,
4040
'si-LK': /^[\u0D80-\u0DFF]+$/,
41+
'ta-IN': /^[\u0B80-\u0BFF]+$/i,
42+
'te-IN': /^[\u0C00-\u0C7F]+$/i,
43+
'kn-IN': /^[\u0C80-\u0CFF]+$/i,
44+
'ml-IN': /^[\u0D00-\u0D7F]+$/i,
45+
'gu-IN': /^[\u0A80-\u0AFF]+$/i,
46+
'pa-IN': /^[\u0A00-\u0A7F]+$/i,
47+
'or-IN': /^[\u0B00-\u0B7F]+$/i,
4148
};
4249

4350
export const alphanumeric = {
@@ -79,6 +86,13 @@ export const alphanumeric = {
7986
eo: /^[0-9ABCĈD-GĜHĤIJĴK-PRSŜTUŬVZ]+$/i,
8087
'hi-IN': /^[\u0900-\u0963]+[\u0966-\u097F]*$/i,
8188
'si-LK': /^[0-9\u0D80-\u0DFF]+$/,
89+
'ta-IN': /^[0-9\u0B80-\u0BFF.]+$/i,
90+
'te-IN': /^[0-9\u0C00-\u0C7F.]+$/i,
91+
'kn-IN': /^[0-9\u0C80-\u0CFF.]+$/i,
92+
'ml-IN': /^[0-9\u0D00-\u0D7F.]+$/i,
93+
'gu-IN': /^[0-9\u0A80-\u0AFF.]+$/i,
94+
'pa-IN': /^[0-9\u0A00-\u0A7F.]+$/i,
95+
'or-IN': /^[0-9\u0B00-\u0B7F.]+$/i,
8296
};
8397

8498
export const decimal = {
@@ -128,8 +142,9 @@ for (let locale, i = 0; i < bengaliLocales.length; i++) {
128142
export const dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'];
129143
export const commaDecimal = [
130144
'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'eo', 'es-ES', 'fr-CA', 'fr-FR',
131-
'id-ID', 'it-IT', 'ku-IQ', 'hi-IN', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT',
132-
'ru-RU', 'kk-KZ', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN',
145+
'gu-IN', 'hi-IN', 'hu-HU', 'id-ID', 'it-IT', 'kk-KZ', 'kn-IN', 'ku-IQ', 'ml-IN', 'nb-NO',
146+
'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sr-RS',
147+
'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'tr-TR', 'uk-UA', 'vi-VN',
133148
];
134149

135150
for (let i = 0; i < dotDecimal.length; i++) {

test/validators.test.js

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,100 @@ describe('Validators', () => {
19021902
});
19031903
});
19041904

1905+
it('should validate Tamil alpha strings', () => {
1906+
test({
1907+
validator: 'isAlpha',
1908+
args: ['ta-IN'],
1909+
valid: [
1910+
'அஆஇஈஉஊஎஏஐஒஓஔகஙசஞடணதநபமயரலவழளறனஶஜஷஸஹ',
1911+
'தமிழ்',
1912+
],
1913+
invalid: [
1914+
'தமிழ்123',
1915+
'தமிழ் ',
1916+
'தமிழ்.',
1917+
'abc',
1918+
'',
1919+
],
1920+
});
1921+
});
1922+
it('should validate Telugu alpha strings', () => {
1923+
test({
1924+
validator: 'isAlpha',
1925+
args: ['te-IN'],
1926+
valid: [
1927+
'అఆఇఈఉఊఋఌఎఏఐఒఓఔకఖగఘఙచఛజఝఞటఠడఢణతథదధనపఫబభమయరలవశషసహ',
1928+
'తెలుగు',
1929+
],
1930+
invalid: ['తెలుగు123', 'తెలుగు.', 'abc', ''],
1931+
});
1932+
});
1933+
it('should validate Kannada alpha strings', () => {
1934+
test({
1935+
validator: 'isAlpha',
1936+
args: ['kn-IN'],
1937+
valid: [
1938+
'ಅಆಇಈಉಊಋಎಏಐಒಓಔಕಖಗಘಙಚಛಜಝಞಟಠಡಢಣತಥದಧನಪಫಬಭಮಯರಲವಶಷಸಹಳ',
1939+
'ಕನ್ನಡ',
1940+
],
1941+
invalid: ['ಕನ್ನಡ123', 'ಕನ್ನಡ.', 'abc', ''],
1942+
});
1943+
});
1944+
it('should validate Malayalam alpha strings', () => {
1945+
test({
1946+
validator: 'isAlpha',
1947+
args: ['ml-IN'],
1948+
valid: [
1949+
'അആഇഈഉഊഋഎഏഐഒഓഔകഖഗഘങചഛജഝഞടഠഡഢണതഥദധനപഫബഭമയരലവശഷസഹള',
1950+
'മലയാളം',
1951+
],
1952+
invalid: ['മലയാളം123', 'മലയാളം.', 'abc', ''],
1953+
});
1954+
});
1955+
it('should validate Gujarati alpha strings', () => {
1956+
test({
1957+
validator: 'isAlpha',
1958+
args: ['gu-IN'],
1959+
valid: [
1960+
'અઆઇઈઉઊઋએઐઓઔકખગઘચછજઝટઠડઢણતથદધનપફબભમયરલવશષસહળ',
1961+
'ગુજરાતી',
1962+
],
1963+
invalid: ['ગુજરાતી123', 'ગુજરાતી.', 'abc', ''],
1964+
});
1965+
});
1966+
it('should validate Punjabi alpha strings', () => {
1967+
test({
1968+
validator: 'isAlpha',
1969+
args: ['pa-IN'],
1970+
valid: [
1971+
'ਅਆਇਈਉਊਏਐਓਔਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵਸ਼ਸਹ',
1972+
'ਪੰਜਾਬੀ',
1973+
],
1974+
invalid: ['ਪੰਜਾਬੀ123', 'ਪੰਜਾਬੀ.', 'abc', ''],
1975+
});
1976+
});
1977+
it('should validate Odia alpha strings', () => {
1978+
test({
1979+
validator: 'isAlpha',
1980+
args: ['or-IN'],
1981+
valid: [
1982+
'ଅଆଇଈଉଊଋଌଏଐଓଔକଖଗଘଙଚଛଜଝଞଟଠଡଢଣତଥଦଧନପଫବଭମଯରଲଶଷସହଳ',
1983+
'ଓଡ଼ିଆ',
1984+
],
1985+
invalid: ['ଓଡ଼ିଆ123', 'ଓଡ଼ିଆ.', 'abc', ''],
1986+
});
1987+
});
1988+
it('should validate Bengali alpha strings', () => {
1989+
test({
1990+
validator: 'isAlpha',
1991+
args: ['bn-IN'],
1992+
valid: [
1993+
'অআইঈউঊঋএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ',
1994+
'বাংলা',
1995+
],
1996+
invalid: ['বাংলা123', 'বাংলা.', 'abc', ''],
1997+
});
1998+
});
19051999
it('should validate persian alpha strings', () => {
19062000
test({
19072001
validator: 'isAlpha',
@@ -2699,6 +2793,164 @@ describe('Validators', () => {
26992793
],
27002794
});
27012795
});
2796+
it('should validate Tamil alphanumeric strings', () => {
2797+
test({
2798+
validator: 'isAlphanumeric',
2799+
args: ['ta-IN'],
2800+
valid: [
2801+
'தமிழ்',
2802+
'தமிழ்123',
2803+
'அஆஇஈ123',
2804+
'தமிழ்123.45',
2805+
'123.45',
2806+
'தமிழ்.',
2807+
],
2808+
invalid: [
2809+
'தமிழ் ',
2810+
'abc',
2811+
'',
2812+
],
2813+
});
2814+
});
2815+
2816+
it('should validate Telugu alphanumeric strings', () => {
2817+
test({
2818+
validator: 'isAlphanumeric',
2819+
args: ['te-IN'],
2820+
valid: [
2821+
'తెలుగు',
2822+
'తెలుగు123',
2823+
'అఆఇఈ123',
2824+
'తెలుగు123.45',
2825+
'123.45',
2826+
'తెలుగు.',
2827+
],
2828+
invalid: [
2829+
'abc',
2830+
'',
2831+
],
2832+
});
2833+
});
2834+
2835+
it('should validate Kannada alphanumeric strings', () => {
2836+
test({
2837+
validator: 'isAlphanumeric',
2838+
args: ['kn-IN'],
2839+
valid: [
2840+
'ಕನ್ನಡ',
2841+
'ಕನ್ನಡ123',
2842+
'ಅಆಇಈ123',
2843+
'ಕನ್ನಡ123.45',
2844+
'123.45',
2845+
'ಕನ್ನಡ.',
2846+
],
2847+
invalid: [
2848+
'abc',
2849+
'',
2850+
],
2851+
});
2852+
});
2853+
2854+
it('should validate Malayalam alphanumeric strings', () => {
2855+
test({
2856+
validator: 'isAlphanumeric',
2857+
args: ['ml-IN'],
2858+
valid: [
2859+
'മലയാളം',
2860+
'മലയാളം123',
2861+
'അആഇഈ123',
2862+
'മലയാളം123.45',
2863+
'123.45',
2864+
'മലയാളം.',
2865+
],
2866+
invalid: [
2867+
'abc',
2868+
'',
2869+
],
2870+
});
2871+
});
2872+
2873+
it('should validate Gujarati alphanumeric strings', () => {
2874+
test({
2875+
validator: 'isAlphanumeric',
2876+
args: ['gu-IN'],
2877+
valid: [
2878+
'ગુજરાતી',
2879+
'ગુજરાતી123',
2880+
'અઆઇઈ123',
2881+
'ગુજરાતી123.45',
2882+
'123.45',
2883+
'ગુજરાતી.',
2884+
],
2885+
invalid: [
2886+
'abc',
2887+
'',
2888+
],
2889+
});
2890+
});
2891+
2892+
it('should validate Punjabi alphanumeric strings', () => {
2893+
test({
2894+
validator: 'isAlphanumeric',
2895+
args: ['pa-IN'],
2896+
valid: [
2897+
'ਪੰਜਾਬੀ',
2898+
'ਪੰਜਾਬੀ123',
2899+
'ਅਆਇਈ123',
2900+
'ਪੰਜਾਬੀ123.45',
2901+
'123.45',
2902+
'ਪੰਜਾਬੀ.',
2903+
],
2904+
invalid: [
2905+
'abc',
2906+
'',
2907+
],
2908+
});
2909+
});
2910+
2911+
it('should validate Odia alphanumeric strings', () => {
2912+
test({
2913+
validator: 'isAlphanumeric',
2914+
args: ['or-IN'],
2915+
valid: [
2916+
'ଓଡ଼ିଆ',
2917+
'ଓଡ଼ିଆ123',
2918+
'ଅଆଇଈ123',
2919+
'ଓଡ଼ିଆ123.45',
2920+
'123.45',
2921+
'ଓଡ଼ିଆ.',
2922+
],
2923+
invalid: [
2924+
'abc',
2925+
'',
2926+
],
2927+
});
2928+
});
2929+
2930+
it('should validate Bengali alphanumeric strings', () => {
2931+
test({
2932+
validator: 'isAlphanumeric',
2933+
args: ['bn-IN'],
2934+
valid: [
2935+
'বাংলা',
2936+
'বাংলা১২৩',
2937+
'অআইঈ১২৩',
2938+
'১২৩৪৫৬৭৮৯০',
2939+
'বাংলা১২৩',
2940+
'১২৩৪৫',
2941+
'বাংলা',
2942+
],
2943+
invalid: [
2944+
'abc',
2945+
'বাংলা123',
2946+
'123',
2947+
'বাংলা ১২৩',
2948+
'বাংলা,১২৩',
2949+
'১২৩٫৪৫',
2950+
'',
2951+
],
2952+
});
2953+
});
27022954

27032955
it('should error on invalid locale', () => {
27042956
test({

0 commit comments

Comments
 (0)