Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isNumeric doesn't support scientific notation #2430

Open
benada002 opened this issue Jul 3, 2024 · 3 comments
Open

isNumeric doesn't support scientific notation #2430

benada002 opened this issue Jul 3, 2024 · 3 comments

Comments

@benada002
Copy link

benada002 commented Jul 3, 2024

Describe the bug
👋 Not sure if you consider this a 🐛, but I just noticed that isNumeric doesn't support scientific notation. Eg. this works perfectly fine (returns true):

const testNumber = (200000000000000000000).toString()
validator.isNumeric(testNumber)

But this returns false, even through it's a valid number

// Results in "2e+21"
const testNumberFailing = (2000000000000000000000).toString()
validator.isNumeric(testNumberFailing)

Examples
image

Reproductions
https://runkit.com/embed/xsw1bpnhjcgs

@SuvitsonHarrese
Copy link

SuvitsonHarrese commented Jul 5, 2024

@benada002
I have added a regex to handle scientific notation
^[+-]?([0-9]*[\.?])?[0-9]+(e(\+|\-)[0-9]+)?$

To handle the scientific notation you have to edit isNumeric.js file or you can copy the below code and paste it in that file where I have added the regex function in return statement

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = isNumeric;
var _assertString = _interopRequireDefault(require("./util/assertString"));
var _alpha = require("./alpha");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var numericNoSymbols = /^[0-9]+$/;
function isNumeric(str, options) {
  (0, _assertString.default)(str);
  if (options && options.no_symbols) {
    return numericNoSymbols.test(str);
  }
       return new RegExp("^[+-]?([0-9]*[".concat((options || {}).locale ? _alpha.decimal[options.locale]+"])?[0-9]+$" : ".])?[0-9]+(e(\\+|\\-)[0-9]+)?$")).test(str);

}
module.exports = exports.default;
module.exports.default = exports.default;

@rubiin
Copy link
Member

rubiin commented Aug 25, 2024

@SuvitsonHarrese you can make a pr if you like

ShreySinha02 pushed a commit to ShreySinha02/validator.js that referenced this issue Sep 8, 2024
@ShreySinha02
Copy link

ShreySinha02 commented Sep 8, 2024

@benada002 @SuvitsonHarrese @rubiin #2447 I had made a PR related to this issue. Could you please review it? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants