Solved

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

Posted on 2013-12-27
3
374 Views
Last Modified: 2014-01-02
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
Comment
Question by:stargateatlantis
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39743109
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
 

Author Comment

by:stargateatlantis
ID: 39743383
So how come you can't execute it like this

var myNumber=1111111111;

console.log(myNumber.toPhone);
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39743435
Add parentheses :
var myNumber=1111111111;

console.log(myNumber.toPhone());

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now