Home > DeveloperSection > Forums > How to format telephone number in Telrik MVC Grid Client.Template?
Pravesh Singh

Total Post:411

Points:2881
Posted on    November-20-2014 2:43 AM

 .NET Telerik Grid  Telerik MVC  String 
Ratings:


 1 Reply(s)
 881  View(s)
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.



Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    November-20-2014 6:06 AM

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);

    }


Don't want to miss updates? Please click the below button!

Follow MindStick