6

I'd like to format a number on the fly as user types. There are a couple of usefull plugins like jquery.maskedinput, but none of them is able to create a dynamic mask that would work with all of scenarios:

  • 1000000 -> 1 000 000
  • 100000 -> 100 000
  • 10000 -> 10 000

And so on. Perhaps there is a clever way to achieve this with jquery dynamically as user types? Or maybe I just missed one plugin that does this? :)

2
  • This must be when the user types and not onblur? Commented Jun 24, 2011 at 15:18
  • Yes, since the field will contain a large number it's a help for a user not to make a mistake :) Commented Jun 25, 2011 at 23:00

2 Answers 2

1

There's a nice plugin that can both format and parse numbers like this. jquery-numberformatter

What you would want to do is when the value of the field changes, immediately parse the field value, and set it back into the format you desire.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution is too heavy - I have to include the plugin script and jshashtable script as a dependency, whereas I need to parse 3-5 fields. Each extra dependency is a penalty in google page speed score. I'd rather go for some code snippet or a short plugin I could include in page code directly :)
1

Here is a nice regexp to take the string apart properly: javascript split string at parts

And here is a small experimental plugin that seems to work ok now (thanks to Segaja from #jquery on freenode!):

https://gist.github.com/1047003

To use it just include it in the page code and add:

$("input.someclass").formatInput();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.