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

feat: Add scientific notation support to isNumeric validator #2466

Closed
Closed
Changes from all 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
19 changes: 13 additions & 6 deletions src/lib/isNumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { decimal } from './alpha';

const numericNoSymbols = /^[0-9]+$/;

export default function isNumeric(str, options) {
assertString(str);
if (options && options.no_symbols) {
return numericNoSymbols.test(str);
}
return (new RegExp(`^[+-]?([0-9]*[${(options || {}).locale ? decimal[options.locale] : '.'}])?[0-9]+$`)).test(str);
export default function isNumeric(str, options = { no_symbols: false }) {
assertString(str); // verify if the str is a string, if not report a TypeError

// destructure options to extract the required properties
const { locale, no_symbols } = options;

// deciding the separator upfront (default separator is '.')
const decimalSeparator = locale ? decimal[locale] : '.';

// setting the regex depending on the value of no_symbols
const regex = no_symbols ? numericNoSymbols : `^[+-]?([0-9]*[${decimalSeparator}])?[0-9]+([eE][+-]?[0-9]+)?$`;

return regex.test(str);
}
Loading