How to make it so that if there are more than two zeros in the number, it is separated by spaces, that is: 100 <2, 1 000> 2, 1 000 000> 2, etc. ?

And somehow it is easier to arrange the insertion of the ruble symbol at the end of the input? There is such a solution, but it seems to me a bit wrong, because removed hard value.

function addRub (val) { return val + " ₽"; } function removeRub (val) { val = val.replace(" ₽", ""); val = val.replace("₽", ""); val = val.replace(" ", ""); return val; } $(".test").on("input", function () { var $this = $(this); var val = $this.prop("value"); var newVal = removeRub(val); newVal = addRub(newVal); $this.prop("value", newVal); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="test" value='&#8381;'> 

    1 answer 1

     test.onkeypress = event => { // Control buttons if (event.key.length > 1) return true; test.value = (test.value+event.key) .replace(/\D/g, '') .replace(/(\d)(?=(\d{3})+([^\d]|$))/g, '$1 ') +'₽'; event.preventDefault(); } 
     <input id="test" value='&#8381;'> 

    • Thank you for the answer, but how can you insert some kind of span instead of the ruble icon and give it a picture or picture right away? - YourDeveloper
    • @YourDeveloper Approximately, only in your case is it more logical to place it on the right - ru.stackoverflow.com/questions/712295/… - Darth
    • @YourDeveloper look, for example, how it is done on Avito. There the input goes, and the currency is already written next. The currency in the input is not quite right to write - Alexxosipov
    • @Darth, I know that, but how to make it so that in your example it moves to the right depending on the number of characters in the input? - YourDeveloper
    • @Darth and where does the test come from? This is a variable, as I understand it, when I try to copy to myself, my gulp is disabled. - YourDeveloper