Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Format telephone number??

Reply
Thread Tools

Format telephone number??

 
 
bernadou
Guest
Posts: n/a
 
      01-22-2006
Ok, this is yet again one of those things that is driving me crazy. How do I
use a formatting expression to format a number? Here's what I'm trying, but,
it isn't working.

Text='<%# Eval("fldPhone", "(###)-###-####") %>'

What I'm trying to see happen is the value "3335551212" output as
"(333)-555-1212". The code above outputs "(###)-###-####". I'm obviously
way off here, but, I can't seem to track down a straightforward article on
how to do this. Any help would be appreciated.

Thanks
Bernie
 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      01-22-2006
Hi Bernie,

The syntax you tried is correct for numbers, but if your fldPhone is of type
string you would get the outcome that you got instead of the one you
expected. So to solve it, either convert it to a number before applying this
format string or use a regular expression.

For example in this line of code I remove any character that might prevent
the field from becoming a number, then convert it to a number then convert it
back again to a string using the format string that you already knew:

<asp:TextBox ID="lblPhone" runat="server" Text='<%#
Convert.ToInt64(Eval("fldPhone").ToString().Replac e(" ",
"").Replace(".", "").Replace("-", "").Replace("(", "").Replace(")",
"")).ToString("000-000-0000")
%>'></asp:TextBox>

In the following line I did the same string cleaning process but then used a
regular expression to achieve the same format:

<asp:Label ID="lblPhone" runat="server"
Text='<%#FormatPhone(Eval("fldPhone").ToString()) %>'></asp:Label>

The FormatPhone function is defined as this:

protected string FormatPhone(string strPhone)
{
strPhone = Convert.ToInt64( strPhone.Replace ("
","").Replace(".","").Replace("-","").Replace ("(","").Replace
(")","")).ToString ("0000000000");
string ret= Regex.Replace(strPhone,
@"(?<AreaCode>\d{3})(?<seg1>\d{3})(?<seg2>\d{4} )",
"(${AreaCode})-${seg1}-${seg2}");
return ret;
}

To understand the regular expression syntax you might review this demo on my
website in which I explained the usage of named groups in regular expressions
http://www.webswapp.com/CodeSamples/...medGroups.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"bernadou" wrote:

> Ok, this is yet again one of those things that is driving me crazy. How do I
> use a formatting expression to format a number? Here's what I'm trying, but,
> it isn't working.
>
> Text='<%# Eval("fldPhone", "(###)-###-####") %>'
>
> What I'm trying to see happen is the value "3335551212" output as
> "(333)-555-1212". The code above outputs "(###)-###-####". I'm obviously
> way off here, but, I can't seem to track down a straightforward article on
> how to do this. Any help would be appreciated.
>
> Thanks
> Bernie

 
Reply With Quote
 
 
 
 
bernadou
Guest
Posts: n/a
 
      01-23-2006
Thanks once again! YTM! If you ever find yourself in Denver, I owe you a
beer!

Thanks,
B

"Phillip Williams" wrote:

> Hi Bernie,
>
> The syntax you tried is correct for numbers, but if your fldPhone is of type
> string you would get the outcome that you got instead of the one you
> expected. So to solve it, either convert it to a number before applying this
> format string or use a regular expression.
>
> For example in this line of code I remove any character that might prevent
> the field from becoming a number, then convert it to a number then convert it
> back again to a string using the format string that you already knew:
>
> <asp:TextBox ID="lblPhone" runat="server" Text='<%#
> Convert.ToInt64(Eval("fldPhone").ToString().Replac e(" ",
> "").Replace(".", "").Replace("-", "").Replace("(", "").Replace(")",
> "")).ToString("000-000-0000")
> %>'></asp:TextBox>
>
> In the following line I did the same string cleaning process but then used a
> regular expression to achieve the same format:
>
> <asp:Label ID="lblPhone" runat="server"
> Text='<%#FormatPhone(Eval("fldPhone").ToString()) %>'></asp:Label>
>
> The FormatPhone function is defined as this:
>
> protected string FormatPhone(string strPhone)
> {
> strPhone = Convert.ToInt64( strPhone.Replace ("
> ","").Replace(".","").Replace("-","").Replace ("(","").Replace
> (")","")).ToString ("0000000000");
> string ret= Regex.Replace(strPhone,
> @"(?<AreaCode>\d{3})(?<seg1>\d{3})(?<seg2>\d{4} )",
> "(${AreaCode})-${seg1}-${seg2}");
> return ret;
> }
>
> To understand the regular expression syntax you might review this demo on my
> website in which I explained the usage of named groups in regular expressions
> http://www.webswapp.com/CodeSamples/...medGroups.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "bernadou" wrote:
>
> > Ok, this is yet again one of those things that is driving me crazy. How do I
> > use a formatting expression to format a number? Here's what I'm trying, but,
> > it isn't working.
> >
> > Text='<%# Eval("fldPhone", "(###)-###-####") %>'
> >
> > What I'm trying to see happen is the value "3335551212" output as
> > "(333)-555-1212". The code above outputs "(###)-###-####". I'm obviously
> > way off here, but, I can't seem to track down a straightforward article on
> > how to do this. Any help would be appreciated.
> >
> > Thanks
> > Bernie

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
transferring a telephone #/ownership of a telephone # hdtv? VOIP 1 10-18-2006 12:27 PM
Supply Telephone Voice Modular Adapters,Telephone Modular Couplers,Modular Duplex Jack,Triplex Adapters,Telephone extension Cord samul888@vip.163.com Cisco 1 11-13-2005 09:23 AM
Supply Telephone Voice Modular Adapters,Telephone Modular Couplers,Modular Duplex Jack,Triplex Adapters,Telephone extension Cord samul888@vip.163.com Computer Support 0 11-12-2005 06:22 AM
Supply Telephone Voice Modular Adapters,Telephone Modular Couplers,Modular Duplex Jack,Triplex Adapters,Telephone extension Cord samul888@vip.163.com VOIP 0 11-12-2005 06:22 AM



Advertisments