Add commas for a number in input field while typing
if you wanted to have something like "1 000 000", you can change the code to something like this.
var num = $this.val().replace(/(\s)/g, '');
$this.val(num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 "));
Hope this helps
Rails: Is there a rails trick to adding commas to large numbers?
You want the number_with_delimiter
method. For example:
<%= number_with_delimiter(@number, :delimiter => ',') %>
Alternatively, you can use the number_with_precision
method to ensure that the number is always displayed with two decimal places of precision:
<%= number_with_precision(@number, :precision => 2, :delimiter => ',') %>
Input mask for numeric and decimal
You can use jquery numeric for numbers.
The current version does allow what you're looking for but someone has changed the code a little bit and it works:
HTML
<input class="numeric" type="text" />
JQuery
$(".numeric").numeric({ decimal : ".", negative : false, scale: 3 });
This is the whole source.
And I've prepared this fiddle so you can see how it works.
Convert comma to point as delimiter
What you're doing may not be the best way, so perhaps someone can answer with a better approach. But to get your line working you need to make it actually persist the change.
self.price.to_s.gsub(',', '.').to_f
Will just return the change, but that doesn't go anywhere in a callback!
self.price = self.price.to_s.gsub(',', '.').to_f
# OR
self.price.to_s.gsub!(',', '.').to_f
Will persist the change within the object.
Related Topics
Input Date Format With Dd/Mm/Yyyy in the Input Field
How to Include External JavaScript Library in Reactjs
How to Style HTML Select Option Hover and Selected Option Effects
How to Partition Input Field to Appear as Separate Input Fields on Screen
Onclick JavaScript to Make Browser Go Back to Previous Page
Jquery Validate Phone Number With With Regex
How to Open a Url Link from JavaScript Inside a Google Apps Script HTML Google Site Gadget
How to Hide the Address Bar on Iphone
Angular Conditional Readonly/Disable on Input Fields
Filter Json Object Array on Multiple Values or Arguments JavaScript
How to Reload a Page With React-Router
How to Render a Word Document (.Doc, .Docx) in the Browser Using JavaScript
How to Convert Gmt Time to Ist Time in JavaScript
How to Display an Iframe on Button Click