3

I have an table and has some data.

There is a field name mobile_no in my result object and many has wrong format. I need to reformat these fields with USA standard.

here is some example.

9292023317

i want to format like this

(929) 202-3317

2 Answers 2

5

Well You can can modified object when you during the init the data Example.

In Controller

 if (response.getState() === "SUCCESS"){
  var result = response.getReturnValue();
     for(var i=0; i<result.length;i++) {
       var newPhoneNo = helper.formatPhoneNumber(component,result[i].Phone__c);    
     result[i].Phone__c = newPhoneNo;  
      }
  }

In Helper

formatPhoneNumber: function(component, phone) {
    var s2 = (""+phone).replace(/\D/g, '');
    var m = s2.match(/^(\d{3})(\d{3})(\d{4})$/);
    return (!m) ? null : "(" + m[1] + ") " + m[2] + "-" + m[3];
},
1
  • Don't forget to add component.set("{!v.results}", results);
    – Tariq
    Apr 24, 2017 at 11:36
0

If it is LWC you can try this option lightning-formatted-phone. Please check below link

Ref:https://developer.salesforce.com/docs/component-library/bundle/lightning-formatted-phone/example

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .