Petahhh
Petahhh

Reputation: 287

Numeric Textbox: Display spaces

I have numeric textboxes in html/php/js where users will input numbers with 4 to 7 digits. However I would like the user to see spaces between every third digit.

For example,

        User inputs: 8000

        User sees: 8 000

        User inputs 9100045

        User sees: 9 100 045

How can I implement this so the input remains an int and does not get converted into a string with white space?

Thank you!

Upvotes: 1

Views: 928

Answers (2)

Curtis
Curtis

Reputation: 103348

With a standard textbox, this isn't possible.

Using javascript you could reformat the content after every key press adding spaces where necessary. However, as @Jukka K. Korpela has pointed out, this could become very confusing for the user. An alternative would be to make this change onchange so that the formatting only occurs when the textbox loses focus.

Then server side, remove all the spaces to return an integer.

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

Do this in the PHP. For Eg., if the user enters the input here:

<input type="text" name="number" />

In PHP, get it as:

$number = intval(trim($_POST["number"])); // Get it as number
$number = number_format($number, 2, ' ', ' ');
// The value will be 5 00 000 for $number = 500000
$number = number_format($number);
// The value will be 6 000 000 for $number = 6000000

See Number Format for more information. Hope this helps! :)

Upvotes: 1

Related Questions