Format telephone number??

Discussion in 'ASP .Net Web Controls' started by bernadou, Jan 22, 2006.

  1. bernadou

    bernadou Guest

    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
     
    bernadou, Jan 22, 2006
    #1
    1. Advertising

  2. 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().Replace(" ",
    "").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/aspnet20/RegExp_NamedGroups.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
     
    Phillip Williams, Jan 22, 2006
    #2
    1. Advertising

  3. bernadou

    bernadou Guest

    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().Replace(" ",
    > "").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/aspnet20/RegExp_NamedGroups.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
     
    bernadou, Jan 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Dror

    Telephone Data Format?

    Ed Dror, Feb 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    389
    blackstaronline.net
    Feb 16, 2006
  2. Sirisha
    Replies:
    1
    Views:
    453
    Scott M.
    Nov 17, 2006
  3. Jeff Thur

    How can I format a telephone # in the datagrid

    Jeff Thur, Mar 7, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    122
    Paul D. Fox
    Mar 11, 2005
  4. Fei Liu
    Replies:
    21
    Views:
    2,380
    John Bokma
    Dec 16, 2006
  5. Sirisha
    Replies:
    4
    Views:
    230
    Evertjan.
    Jan 9, 2007
Loading...

Share This Page