Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

Trying to convert this into prototype to format Number into phone number

Having problems getting this prototype function to work to format a number into a phone number format.

Number.prototype.toPhone = function () {
    myStr=this.toString().replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
    return myStr
}

Open in new window

0
stargateatlantis
Asked:
stargateatlantis
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/2pyu6/
Number.prototype.toPhone = function () {
    myStr = this.toString().replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
    return myStr
}

var n = 1234567890;
var phone = new Number(n).toPhone();
alert(phone);

Open in new window

0
 
stargateatlantisAuthor Commented:
So how come you can't execute it like this

var myNumber=1111111111;

console.log(myNumber.toPhone);
0
 
leakim971PluritechnicianCommented:
Add parentheses :
var myNumber=1111111111;

console.log(myNumber.toPhone());

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now