Skip to content

Commit

Permalink
Merge pull request #848 from Maxattax97/numeric
Browse files Browse the repository at this point in the history
Add noSymbols option to isNumeric
  • Loading branch information
chriso authored Jul 31, 2018
2 parents 79a74a2 + 664d2b4 commit 0f4877b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Validator | Description
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str)** | check if the string contains only numbers.
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{noSymbols: false}`, meaning that it will validate strings that do contain any of `+`, `-`, or `.`.
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. locale list is `validator.isPostalCodeLocales`.).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
Expand Down
6 changes: 5 additions & 1 deletion lib/isNumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ var _assertString2 = _interopRequireDefault(_assertString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
var numericNoSymbols = /^[0-9]+$/;

function isNumeric(str) {
function isNumeric(str, options) {
(0, _assertString2.default)(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion src/lib/isNumeric.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import assertString from './util/assertString';

const numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
const numericNoSymbols = /^[0-9]+$/;

export default function isNumeric(str) {
export default function isNumeric(str, options) {
assertString(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}
26 changes: 26 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,34 @@ describe('Validators', () => {
'-0',
'+123',
'123.123',
'+000000',
],
invalid: [
' ',
'',
'.',
],
});
});

it('should validate numeric strings without symbols', () => {
test({
validator: 'isNumeric',
args: [{
noSymbols: true,
}],
valid: [
'123',
'00123',
'0',
],
invalid: [
'-0',
'+000000',
'',
'+123',
'123.123',
'-00123',
' ',
'.',
],
Expand Down
6 changes: 5 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,13 @@ function isAlphanumeric(str) {
}

var numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
var numericNoSymbols = /^[0-9]+$/;

function isNumeric(str) {
function isNumeric(str, options) {
assertString(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}

Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 0f4877b

Please sign in to comment.