Jonas
Jonas

Reputation: 261

How to insert comma after 3 number in react

I am working on input , I want to insert comma after 3 number like ( 352,353,353 ) . I want to show it in text field . I am new to react could someone please help me how to achieve this goal .

Code

this.state = {
 phoneNumber:''
}

  <input type="text" placeholder="Enter text" value={this.state.phoneNumber} />

Upvotes: 0

Views: 507

Answers (1)

WinjayYu
WinjayYu

Reputation: 26

You can use number.toLocaleString()

Based on your example

this.state = {
 phoneNumber:''
}

<input type="text" placeholder="Enter text" value={this.state.phoneNumber} onChange={(e) => this.setState({phoneNumber: Number(e.target.value.replace(/,/g, '')).toLocaleString()})}/>

Upvotes: 1

Related Questions