Skip to content

Commit 9edb2ca

Browse files
Merge d313ed6 into be8874d
2 parents be8874d + d313ed6 commit 9edb2ca

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ Validator | Description
8484
**contains(str, seed [, options ])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false}`.<br/>`ignoreCase` specified whether the case of the substring be same or not.
8585
**equals(str, comparison)** | check if the string matches the comparison.
8686
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
87-
**isAlpha(str [, locale])** | 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', 'az-AZ', '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', 'es-ES', 'fr-FR', 'fa', 'fa-AF', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`.
88-
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<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', 'az-AZ', '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', 'es-ES', 'fr-FR', 'fa', 'fa-AF', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
87+
**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', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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.
88+
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<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', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fa-IR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
8989
**isAscii(str)** | check if the string contains ASCII chars only.
9090
**isBase32(str)** | check if a string is base32 encoded.
9191
**isBase58(str)** | check if a string is base58 encoded.

src/lib/isAlpha.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import assertString from './util/assertString';
22
import { alpha } from './alpha';
33

4-
export default function isAlpha(str, locale = 'en-US') {
5-
assertString(str);
4+
export default function isAlpha(_str, locale = 'en-US', options = {}) {
5+
assertString(_str);
6+
7+
let str = _str;
8+
const { ignore } = options;
9+
10+
if (ignore) {
11+
if (ignore instanceof RegExp) {
12+
str = str.replace(ignore, '');
13+
} else if (typeof ignore === 'string') {
14+
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
15+
} else {
16+
throw new Error('ignore should be instance of a String or RegExp');
17+
}
18+
}
19+
620
if (locale in alpha) {
721
return alpha[locale].test(str);
822
}

test/validators.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,45 @@ describe('Validators', () => {
925925
});
926926
});
927927

928+
it('should validate alpha string with ignored characters', () => {
929+
test({
930+
validator: 'isAlpha',
931+
args: ['en-US', { ignore: '- /' }], // ignore [space-/]
932+
valid: [
933+
'en-US',
934+
'this is a valid alpha string',
935+
'us/usa',
936+
],
937+
invalid: [
938+
'1. this is not a valid alpha string',
939+
'this$is also not a valid.alpha string',
940+
'this is also not a valid alpha string.',
941+
],
942+
});
943+
944+
test({
945+
validator: 'isAlpha',
946+
args: ['en-US', { ignore: /[\s/-]/g }], // ignore [space -]
947+
valid: [
948+
'en-US',
949+
'this is a valid alpha string',
950+
],
951+
invalid: [
952+
'1. this is not a valid alpha string',
953+
'this$is also not a valid.alpha string',
954+
'this is also not a valid alpha string.',
955+
],
956+
});
957+
958+
test({
959+
validator: 'isAlpha',
960+
args: ['en-US', { ignore: 1234 }], // invalid ignore matcher
961+
error: [
962+
'alpha',
963+
],
964+
});
965+
});
966+
928967
it('should validate Azerbaijani alpha strings', () => {
929968
test({
930969
validator: 'isAlpha',

validator.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function _unsupportedIterableToArray(o, minLen) {
9494
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
9595
var n = Object.prototype.toString.call(o).slice(8, -1);
9696
if (n === "Object" && o.constructor) n = o.constructor.name;
97-
if (n === "Map" || n === "Set") return Array.from(o);
97+
if (n === "Map" || n === "Set") return Array.from(n);
9898
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
9999
}
100100

@@ -1056,9 +1056,22 @@ function isLocale(str) {
10561056
return localeReg.test(str);
10571057
}
10581058

1059-
function isAlpha(str) {
1059+
function isAlpha(_str) {
10601060
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
1061-
assertString(str);
1061+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1062+
assertString(_str);
1063+
var str = _str;
1064+
var ignore = options.ignore;
1065+
1066+
if (ignore) {
1067+
if (ignore instanceof RegExp) {
1068+
str = str.replace(ignore, '');
1069+
} else if (typeof ignore === 'string') {
1070+
str = str.replace(new RegExp("[".concat(ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&'), "]"), 'g'), ''); // escape regex for ignore
1071+
} else {
1072+
throw new Error('ignore should be instance of a String or RegExp');
1073+
}
1074+
}
10621075

10631076
if (locale in alpha) {
10641077
return alpha[locale].test(str);

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)