Soumya Behera
Soumya Behera

Reputation: 2512

How to unmask US mobile number format while making an API call in react js?

While sending data to server I want to unmask us formatted mobile number from (222) 2222-2222 to 2222222222. Can anyone please suggest me how to achieve this?

Below my code to format as US mobile number format.

handleChange(e) {
    var formattedNumber = e.target.value.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
}

Now its formatting as (xxx) xxxx-xxxx.

How I will unmask the formatted number while sending to server?

Upvotes: 1

Views: 1231

Answers (1)

DroidNoob
DroidNoob

Reputation: 1143

Try this.

var str = "(222) 2222-2222 ";
console.log(str.replace(/[^\d]/g, ""));

So during the submit action you could change formattedNumber using the above String.replace() method.

Upvotes: 2

Related Questions