Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2013-12-27
3
378 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding Row and Cell on a table 14 25
Html form and modal / img src -problem 3 30
How to reload page and grab querystring 2 22
html input 8 43
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

828 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