HOW TO FORMAT TELEPHONE NUMBER IN TELRIK MVC GRID CLIENT.TEMPLATE?

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
 1853  View(s)
Ratings:
Rate this:

How can i format Telephone number from xxxxxxxxxx to (xxx) xxx-xxxx

in telerik MVC Grid .client.template.

I tried something like this. which is not working

string formatPattern = @"(\d{3})(\d{3})(\d{4})";

 columns.Bound(e=>e.ID)

 .ClientTemplate(Html.Raw(String.Format("{0:###-###-####}", "<#= Telephone #>") + "<br />"      + Regex.Replace("<#= Fax #>", formatPattern, "($1) $2-$3")).ToString()).Title("Phone / Fax");

I tried both ways String.format and Regex.Replace

I also tried $.telerik.formatString('{0:###-###-####}'<#= telephone =>)

but no use.

  1. Samuel Fernandes

    Post:159

    Points:1117
    Re: How to format telephone number in Telrik MVC Grid Client.Template?

    I got this done with the below code.

    In Telerik Grid bound the column like this

     .ClientTemplate("<#= GetPhoneFaxTemplate(data) #>").Title("Phone / Fax");

    And the Scripts goes like this

    function GetPhoneFaxTemplate(data) {
            if(isNumeric(data.Telephone)) {
                var phone= $.telerik.formatString('{0:(###) ###-####}', Number(data.Telephone))
            }
            else {
                phone =data.Telephone;
            }
            if (isNumeric(data.Fax)) {
                var fax =$.telerik.formatString('{0:(###) ###-####}', Number(data.Fax))
            }
            else {
                fax =data.Fax;
            }
            template =phone + "<br />" + fax;
            return template;
        }
        function isNumeric(n) {
            return !isNaN(parseFloat(n)) && isFinite(n);
        }

      Modified On Apr-06-2018 11:13:02 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!