Skip to content

Fix InputNumber validation for scientific notation values - #67988

Merged
kotlarmilos merged 21 commits into
dotnet:mainfrom
surya3655:6326-incorrect-validation
Aug 14, 2026
Merged

kotlarmilos merged 21 commits into
dotnet:mainfrom
surya3655:6326-incorrect-validation

Conversation

@surya3655

Copy link
Copy Markdown
Contributor

Description

This change fixes validation behavior for floating-point values entered in scientific notation in Blazor binding.

Previously, BindConverter parsed float, float?, double, and double? values using NumberStyles.Number. That parsing style supports normal numeric values and group separators, but it does not allow exponent/scientific notation.

Because of this, valid floating-point values such as the following were rejected by InputNumber<T> validation:

1e-6
2E-06
3.5e10

The validation error shown was similar to:

The MyDouble field must be a number.

This update changes the parsing flow so that:

  • float and double values support scientific notation.
  • nullable float? and double? values support scientific notation.
  • existing parser-level group separator support is preserved by using NumberStyles.AllowThousands.
  • invalid or incomplete exponent values still fail validation.
  • regression tests cover scientific notation parsing and validation scenarios.

The parsing style has been updated from:

NumberStyles.Number

to:

NumberStyles.Float | NumberStyles.AllowThousands

This ensures valid floating-point values written in exponent notation are accepted consistently with .NET floating-point parsing behavior.


Output

Before

image

After

image

Fix Details

Updated BindConverter parsing for the following types:

float
float?
double
double?

Changed float parsing from:

float.TryParse(
    text,
    NumberStyles.Number,
    culture ?? CultureInfo.CurrentCulture,
    out var converted)

to:

float.TryParse(
    text,
    NumberStyles.Float | NumberStyles.AllowThousands,
    culture ?? CultureInfo.CurrentCulture,
    out var converted)

Changed double parsing from:

double.TryParse(
    text,
    NumberStyles.Number,
    culture ?? CultureInfo.CurrentCulture,
    out var converted)

to:

double.TryParse(
    text,
    NumberStyles.Float | NumberStyles.AllowThousands,
    culture ?? CultureInfo.CurrentCulture,
    out var converted)

Tests Added

Added regression coverage for:

  • double scientific notation parsing.
  • double? nullable empty/null behavior.
  • parser-level group separator support.
  • invalid or incomplete scientific notation values.
  • InputNumber<double?> scientific notation and empty value behavior.
  • InputNumber<double> invalid scientific notation validation behavior.

Representative valid test values:

1e-6
2E-06
3.5e10
+4E8

Representative invalid test values:

2E
2E-
2E+
1ee6
1e--6
1e++6

Notes

NumberStyles.Float is required because it includes exponent/scientific notation support.

NumberStyles.AllowThousands is included to preserve existing parser-level support for group separators, such as:

1,234
-1,234.56

This change does not modify parsing behavior for integer or decimal types.


Fixes #6326.

@surya3655
surya3655 requested a review from a team as a code owner July 24, 2026 07:07
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 24, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @surya3655. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@surya3655
surya3655 force-pushed the 6326-incorrect-validation branch from 661b4c0 to e8ffc63 Compare July 24, 2026 07:08
@gfoidl gfoidl added the area-blazor Includes: Blazor, Razor Components label Jul 24, 2026
@surya3655
surya3655 force-pushed the 6326-incorrect-validation branch from e8ffc63 to 749ed45 Compare July 24, 2026 09:01
@surya3655
surya3655 force-pushed the 6326-incorrect-validation branch from 749ed45 to 4404b6a Compare July 28, 2026 03:06
…655/aspnetcore-main into 6326-incorrect-validation

# Conflicts:
#	src/Components/Components/src/BindConverter.cs
#	src/Components/Components/test/BindConverterTest.cs
@surya3655
surya3655 requested review from a team and wtgodbe as code owners July 29, 2026 07:16
Copilot AI lite review requested due to automatic review settings August 10, 2026 03:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Blazor’s binding/conversion pipeline so InputNumber<T> validation accepts floating-point values entered in scientific notation (for float, double, and their nullable variants), aligning behavior with common .NET parsing expectations and addressing #6326.

Changes:

  • Updated BindConverter float/double parsing to allow exponent notation while preserving prior numeric parsing behaviors.
  • Added BindConverter regression tests for scientific notation, group separators, and invalid exponent inputs.
  • Added InputNumber<double> / InputNumber<double?> validation regression tests covering scientific notation and empty inputs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Components/Web/test/Forms/InputNumberTest.cs Adds InputNumber<double> / InputNumber<double?> validation regression tests for exponent notation and invalid inputs.
src/Components/Components/test/BindConverterTest.cs Adds parsing regression tests for scientific notation, group separators, and invalid exponent formats.
src/Components/Components/src/BindConverter.cs Expands float/double parsing styles to accept exponent notation during conversion.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@kotlarmilos

Copy link
Copy Markdown
Member

Thanks, looks good. Could you please reduce the number of tests or variants? Let's just test one scenario with several inputs, no need to have the full coverage here

@surya3655

Copy link
Copy Markdown
Contributor Author

@kotlarmilos Thanks for the feedback! I've refactored the test suite to follow your recommendation—consolidated BindConverterTest from 6 methods down to 3, reducing total test variants from 28 to 12 (57% reduction). All conversion scenarios are preserved in single focused tests using [Theory] with representative [InlineData] cases. Removed 2 redundant InputNumber tests since BindConverter already validates conversion logic, keeping 5 component-level tests intact. Both test projects pass successfully.

@kotlarmilos kotlarmilos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

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

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect validation of floating point data

4 participants