dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.28k stars 9.96k forks source link

Incorrect validation of floating point data #6326

Open AlbertoPa opened 5 years ago

AlbertoPa commented 5 years ago

Data of floating point types are incorrectly forced to be numbers in general format by client-side validation, without accounting for the possibility of values displayed or inserted in scientific notation. The data-val-number tag is automatically added to, and cannot be trivially removed from the generated HTML. This causes an inconsistency because it suffices to define a default value small or big enough to be displayed in scientific notation (say, 1.0e-5) to receive a validation error at submission even if the entry is perfectly valid.

To Reproduce

Steps to reproduce the behavior:

  1. Create a model with a double data member.
  2. Scaffold a Razor Page with CRUD for the model at point 1.
  3. Try to insert a value in scientific notation in the text box for the variable of double type.
  4. See error

Expected behavior

There are two possibilities: a) values in scientific notation are accepted b) a custom validator should be able to override the data-val-number setting

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @AlbertoPa. We think this is a limitation with jQueryValidation library we rely on for Client validation. If you think this can be addressed in our library, we'll happily consider a PR.

AlbertoPa commented 4 years ago

This issues seems also to affect Blazor, where jQuery is not used.

Steps to reproduce:

  1. In a component, add a double property and initialize it to a number in scientific notation. For example: public double myDouble {get; set;} = 1.0e-6;
  2. Bind myDouble to an InputNumber control
  3. Edit the control, changing the value to 2.0e-6 for example

What happens The validation fails because 1.0e-6 is not recognized as a number, even if the user was correctly presented with the number if scientific notation.

Expected behavior The validation succeed if a number in scientific notation is inserted.

AlbertoPa commented 4 years ago

A note on this: using a custom validator does not resolve the issue if the type is double because the default validation is still triggered. This can be easily verified in Blazor too.

The only solution I have found at this point is to create a new input control, which seems a bit excessive for a primitive type.

rummelsworth commented 2 years ago

Just throwing in my user voice here. My team maintains a calculation-intensive web app with hundreds of numeric inputs using InputNumber controls bound to double properties, and the engineers and scientists using the app expect to be able to use exponential formatting in the numeric text they enter. So for us, this is of at least medium severity. Not critical, but possibly high, and at least medium.