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 #2467

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

VivekHalder
Copy link

@VivekHalder VivekHalder commented Oct 1, 2024

Pull Request: Enhancing Numeric Validation with Scientific Notation and Locale Support

This pull request introduces significant enhancements to the isNumeric function, improving its ability to validate more complex numeric formats, including scientific notation and locale-aware decimal separators.

Key Enhancements:

1. Scientific Notation Support

The isNumeric function now fully supports scientific notation, making it easier to validate numbers in exponential form. Inputs such as 1e5, 1.23E-5, and similar formats with both positive and negative exponents are correctly identified as valid numeric strings. This improvement ensures the validation of scientific data and other formats that utilize exponents.

Examples:

  • 1e5 (Scientific notation with a positive exponent)
  • 1.23E-5 (Scientific notation with a decimal and negative exponent)

2. Locale-Aware Decimal Separators

We have introduced a locale-aware option, allowing isNumeric to handle different decimal separators based on region-specific formats. This makes numeric validation more flexible and adaptable to various international standards.

Examples:

  • 1,23e5 (Where , is used as the decimal separator in certain locales)

This enhancement adds a more global-friendly approach, accommodating numeric formats that vary by region.

3. Backward Compatibility

The no_symbols option remains intact to ensure that existing behavior is preserved. This option can still be used to restrict symbols like + and -, ensuring no regressions for users who rely on this setting.

Fixes:

Fixes #2463 .

Checklist:

  • PR contains only changes related to this feature; no unrelated files.
  • README updated to reflect new options.
  • References provided in the PR description where applicable.

Usage

To validate numbers in scientific notation or locale-specific formats, pass the appropriate options to the isNumeric function. The function now seamlessly handles both traditional and scientific numeric formats, offering a wider range of use cases.

Example Usage:

validator.isNumeric('1e5');          // true (Scientific notation)
validator.isNumeric('1.23E-5');      // true (Scientific notation with decimal and negative exponent)
validator.isNumeric('1,23e5', { locale: 'de-DE' }); // true (Locale-aware decimal separator)

- Updated the isNumeric function to handle scientific notation values.
- Modified the regular expression to account for both positive and negative exponents using 'e' or 'E'.
- Introduced an option to handle locales with different decimal separators.
- Preserved backward compatibility by ensuring the no_symbols option continues to function correctly.
- This change validates inputs like 1e5, 1.23E-5, and other variations of scientific notation.
- Removed unexpected spaces inside template literals in the isNumeric function.
- This fix addresses ESLint errors related to the `template-curly-spacing` rule, ensuring code style consistency.
- The changes allow the code to pass linting checks, improving overall code quality.
…unction

Changed the regular expression construction in the isNumeric function to avoid using a string literal directly. The decimal separator is now properly escaped in the regex pattern.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

isNumeric does not handle Scientific Notation
1 participant