Alex Rivera | Logout

Insert hyphens in JavaScript

Asked 2011-08-08T11:34:33.707
32

What is the easiest way to insert hyphens in JavaScript?

I have a phone number eg. 1234567890

While displaying in the front-end, I have to display it as 123-456-7890 using JavaScript.

What is the simplest way to achieve this?

Edit
Report

1 Answer

6

You could use the substr-function to achieve this, assumed that the hyphens are always inserted on the same position:

var hypString = phonestr.substr(0,3) + '-' + phonestr.substr(3, 6) + '-' + phonestr.substr(6);
answered 2011-08-08T11:39:19.240

Your Answer