0

How to restrict textbox to allow only numbers and format as US mobile number format in react js ? ex : (456) 125-0156. Below code to allow only numbers.

mobileNumberChange(evt) {
    var decimalvalidate = /^[2-9][0-9]*$/;
    if (!decimalvalidate.test(evt.key)) {
      evt.preventDefault();
}

But how will I format as (456) 125-0156 ?

Soumya Behera
  • 2,325
  • 4
  • 15
  • 25

1 Answers1

1

By the looks of it, you're looking for a regexp to validate US phone numbers, which will be

/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/

Refer to this answer for detailed explanation

Community
  • 1
  • 1
Hunaid Hassan
  • 732
  • 5
  • 13