Formatting the phone number

前端 未结 3 445
离开以前
离开以前 2020-12-12 00:13

The below textbox is printing the value of the phone number after getting it from the database.

3条回答
  •  天命终不由人
    2020-12-12 00:46

    I would prefer to use replace and regexp (less code, more features).

             var phone = "0123456789";
             phone.replace(/(\d{3})(\d{3})(\d{4})/,"($1)$2-$3"); // (012)345-6789
    

    Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

提交回复
热议问题