Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

input [number] doesn't evaluate stringified numbers #1126

Closed
ex-nerd opened this issue Jul 7, 2012 · 6 comments
Closed

input [number] doesn't evaluate stringified numbers #1126

ex-nerd opened this issue Jul 7, 2012 · 6 comments

Comments

@ex-nerd
Copy link

ex-nerd commented Jul 7, 2012

For a given input:

<input type="number" ng-model="mynum" />

The following really should be true:

$scope.mynum = 5;
$scope.mynum = 5.0;
$scope.mynum = '5';
$scope.mynum = '5.00';

Instead, the second results in 5 (not the intended 5.0) and the last 2 result in a blank field presumably due to falsely failed validation.

This makes it basically impossible to use a text field for a price, to which I would like to apply outside formatting (via an angular-ui blur event) to make sure my prices have the correct 2 decimal places that users expect.

Please consider using a simple /^-?\d+(.\d*)?$/ regex to validate inputs to number fields set by outside forces rather than whatever you currently use (I believe you're already doing something similarly flexible when validating user-supplied input).

@pkozlowski-opensource
Copy link
Member

@petebacondarwin
Copy link
Contributor

How about using a normal text field and creating some kind of currency validator directive that will do the formatting, conversion and check the validity at the same time?

@ex-nerd
Copy link
Author

ex-nerd commented Dec 16, 2012

Rounded decimal numbers aren't always currency.

@petebacondarwin
Copy link
Contributor

@ex-nerd : Oops sorry, I was mixing up my issues. There was another one that was talking about putting currency values into an input. But my general idea still holds. Make a "decimal" directive that will do validation etc.

@pkozlowski-opensource
Copy link
Member

@ex-nerd the input type number directive doesn't apply any special formatting - it just displays a value as given by JavaScript. If you do the following:

var price = 5.00;
console.log(price);

you will get 5 as the result.

Having said this AngularJS allows you to define whatever formatting you need. It is enough to create a simple directive that will plug into the NgModelController#$formatters pipeline as described here: http://docs.angularjs.org/guide/forms

This approach makes sense since AngularJS can accommodate for all the formatting needs.

Here is the jsFiddle that shows as simple price directive that delegates to the number filter to output model value in a desired format: http://jsfiddle.net/MS7Qd/

Not sure what else could be done by default on the AngularJS side, for me a custom formatting directive is a way to go.

@pkozlowski-opensource
Copy link
Member

Closing for now, you can create a custom formatter as shown in the jsfiddle.

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

No branches or pull requests

3 participants