Solved

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

Posted on 2013-12-27
3
372 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
Comment Utility
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
Comment Utility
So how come you can't execute it like this

var myNumber=1111111111;

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

Expert Comment

by:leakim971
Comment Utility
Add parentheses :
var myNumber=1111111111;

console.log(myNumber.toPhone());

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now