Lots of thanks, Donncha, for this neat function. Works great!
Inge
> Hi Inge, here's a function you could use:
>
> function formatInt ( ctrl )
> {
> var separator = " ";
> var int = ctrl.value.replace ( new RegExp ( separator, "g" ), "" );
> var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
> do
> {
> int = int.replace ( regexp, separator + "$1" );
> }
> while ( int.search ( regexp ) >= 0 )
> ctrl.value = int;
> }
>
> I've used a space to separate each thousand, but you can use whatever
you
> like by changing the value in "separator".
> The function takes the value in the form field specified in the ctrl
> parameter ( you can use something like this: <input type="text"
> onkeyup="formatInt ( this )"> to reference the text box, note that you
> must use onKeyUp as the event, otherwise the last entered digit will not
> register ), it then removes any previously formatted separators, and
> declares a new regular expression. The regexp looks for 3 digits after a
> non-word boundary (e.g. another digit), followed by either the end of
the
> string or one of our separators. I've used a do...while loop as it was
the
> easiest way I could think of to ensure the spaces were inserted in
exactly
> the right places.
>
> > Hi,
> >
> > My web application deals with great (integer) numbers. I therefore
need
> a
> > JavaScript function that put in a thousand separator when the
> > user "onchange" leaves one of my text boxes, in this case a blank as in
> > 1 234 567. A better alternative would perhaps be formating digit by
> > digit "onKeyPress". Anybody who has a tip on how to format a number
like
> > this (preferably "onKeyPress")?
> >
> > Regards,
> > Inge Larsson