0

In my jsf page, I have an inputtext with a string value. The input should be numbers only but I want it to automatically be formatted after I enter the data. For example:

I inputted 1000, after entering the data it will be formatted to 1,000

My input text looks like so:

<h:inputText value="#{Bean.myInputText}" styleClass="strFormatter">
   <p:ajax event="blur" update="@this"/>
</h:inputText>

My script looks like so:

$(".strFormatter").blur (
   function() {
    var strToFormat = $(this).val();
    return strToFormat.replace(/(\d)(?=(\d{3})+(?1\d))g, "$1,");
})

But this does not work so my question is, how can I format my string of numbers automatically to be comma-separated after input without triggering any actions?

user1597438
  • 2,171
  • 5
  • 35
  • 78

1 Answers1

2

Try this example:

var n = 1000;
console.log(n.toLocaleString()); //<-- prints: "1,000"

n = 234234;
console.log(n.toLocaleString()); //<-- prints: "234,234"

enter image description here