Reputation: 409
I have an input in my form where the user have to write an amount to pay. The problem is that the user have different ways to do it, it could be 1,350.55 (this is he correct one), but it could be something like this 1.350,55 or 1.350. So, is there any way to parse the amount to my correct form?
Thanks!
Upvotes: 0
Views: 195
Reputation: 22323
Attach ng-pattern to your input and feed it a RegEx of your required pattern. This way people will only be able to input in the correct way and you can avoid bad data.
Please check out this link for more information on input formatting.
Here I have an example for any whole number up to 9999999
its very simple if you understand RegEx
<input type="number" ng-model="price" name="price_field"
ng-pattern="/^[0-9]{1,7}$/" required>
You can check your RegEx here.
Upvotes: 0
Reputation: 358
You want to parse the input, right? Why not try to make the input a currency field? And then parse it into a container through angular, so that customers may see the value and then work with the angular filtered value.
Here is a fiddle: http://jsfiddle.net/lacrioque/vouLmrac/
<p><label for='numberinput'>Your Price here: </label><input type='number' name='numberinput' ng-model='numbertofilter' placeholder="1,350.99"/></p>
<p>Price: <span>{{numbertofilter | currency }}</span></p>
Upvotes: 2
Reputation: 16435
If you don't know the format, how would you parse 13.995
? Would it be "13955" (an integer) or the float one?
You may instead want to mask the format and avoid thousand separators entirely. And tell the user not to use them.
Upvotes: 0