0

How would I format a number like: (99) 9999-9999 into: 9999999999 using angularjs? someone told me to use phoneformat.js but I don't know how to implement it in my project

1
  • I would start with the Install Instructions and then move down to FAQ. Finally, check out the Demo portion. Then try it out and then we will help when you get stuck :) Commented Mar 13, 2015 at 20:20

4 Answers 4

1

I'm not sure that you need anything special from Angular or any special libraries . . . just use the basic JS .replace() method and a little regex:

var sPhoneNum = "(99) 9999-9999";
var sFormattedPhoneNum = sPhoneNum.replace(/\D/g, "");
// sFormattedPhoneNum equals "9999999999"

The regular expression /\D/g matches all non-numeric characters, so it will strip out everything but the numbers.

Sign up to request clarification or add additional context in comments.

3 Comments

I guess the angular way would be just to put this in a filter :) but I agree this would be the best and easiest way... no need to start including external files just to strip down all none numeric...this is the correct answer, just put it in a custom filter and puff... its angularized:)
Yeah, good point . . . you do actually have to plug it into your angular code for it to work. :) But, yeah, this is pretty simple formatting logic . . . certainly no need for any heavy-duty tools to get it done.
yup:) thats why I up voted the answer :) I just added the filter answer below should he want a filter...
1

so like talemyn said... the solution is simply to remove the unwanted char... the angular way to do it is via filter I guess... this is a jsfillde with an example...

myApp.filter('phoneToNum', function() {
    return function(input, scope) {
        return input.replace(/\D/g, "");
    }
});

now if you also want to revert it... use phone filter

Comments

0

Try this:

formatLocal('US', phoneNumber)

Comments

0

I had an input field where I needed to get phone number from users but only the digits so that it's easy to index using phone number in the database. I used .replace on ng-keyup, so the non-digit characters gets removed as the user types.

in html

<input ng-keyup="formatNum()" ng-model='data.phone' placeholder="Phone Number" />  

in controller

$scope.formatNum = function() {
 if ($scope.data.phone)
      $scope.data.phone = $scope.data.phone.replace(/\D/g, "");
};

1 Comment

if no save in this form +9334 46437658 0093 3446437658 03446437658 get phone no in this form 3446437658

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.