TextBoxFor with a thousand separator data annotation

1.1k Views Asked by At

I'd like to get a TextBoxFor with a thousand separator.

In an older version of my project, I did it with Javascript

$('.thousandSeparator').keyup(function () {
    $(this).val(separator.call($(this).val().split(' ').join(''), ' ', '.'));
});

This works, but I don't want to rely on JS and do it only server-side.

So I found :

[DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
public decimal MyProperty { get; set; }

But this doesn't work.

What I'd like is, when the user is typing, a thousand separator (a space in my case) appears at the 4th, 7th, 10th... number typed in the textbox.

This is supposed to work in a very basic "Create" or "Update" form. However, the DisplayFormat data annotation just pre-fill my textbox with 0,00, but if the user types "1000" I'd like it would be transformed as "1 000".

Is it possible without using JS ?

0

There are 0 best solutions below