Formatting data being displayed

Discussion in 'Javascript' started by tanhaa, Sep 22, 2006.

  1. tanhaa

    tanhaa Guest

    Hi all,

    Just curious if Javascript can do this for me:

    I have data stored in a particular format and when displaying that
    data, I want it to be shown in specific format.

    E.G. Phone numbers:

    the numbers are stored in a string format: 3335551212

    When the number is displayed, I want it to be displayed as: (333)
    555-1212

    I know there are several ways where you can ensure that validation is
    done when the data is being inserted so that the number can be stored
    in (333) 555-1212 format, but i dont want the data to be stored like
    that, I only want it to be displayed like that.

    All my search on the web to see if this is possible has been really
    fruitless. Can anyone please help?

    Amit Malhotra
     
    tanhaa, Sep 22, 2006
    #1
    1. Advertising

  2. tanhaa

    Randy Webb Guest

    tanhaa said the following on 9/22/2006 3:29 PM:
    > Hi all,
    >
    > Just curious if Javascript can do this for me:


    <snip>

    > the numbers are stored in a string format: 3335551212
    >
    > When the number is displayed, I want it to be displayed as: (333)
    > 555-1212


    function formatUSPhoneNumber(numToFormat){
    areaCode = numToFormat.substring(0,3)
    prefix = numToFormat.substring(3,6)
    suffix = numToFormat.substring(6,10)
    return("(" + areaCode + ") " + prefix + "-" + suffix)
    }

    document.write(formatUSPhoneNumber('3335551212'))

    Ouputs:

    (333) 555-1212

    Note the space after the ). If you don't want it there, remove it in the
    return line of the function.
    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Sep 22, 2006
    #2
    1. Advertising

  3. tanhaa

    ASM Guest

    tanhaa a écrit :
    > Hi all,
    >
    > Just curious if Javascript can do this for me:
    >
    > I have data stored in a particular format and when displaying that
    > data, I want it to be shown in specific format.


    On what moment do you want this ?
    - during loading ?
    - after loading ?
    - for one or several numbers ?

    > the numbers are stored in a string format: 3335551212
    >
    > When the number is displayed, I want it to be displayed as:
    > (333) 555-1212


    In header of your page :

    <script type="text/javascript">

    function toPhone(m) {
    m = '('+m.substring(0,3)+') '+
    m.substring(3,6)+'-'+
    m.substring(6);
    return m;
    }

    // example to delete after test :
    alert(toPhone('3335551212'));

    </script>

    > I only want it to be displayed like that.


    so, OK no verification on this data

    > All my search on the web to see if this is possible has been really
    > fruitless. Can anyone please help?


    Phone number formatted during loading,
    add where you want it :

    <script type="text/javascript">
    document.write(toPhone('3335551212'));
    </script>

    --
    ASM
     
    ASM, Sep 22, 2006
    #3
  4. tanhaa

    tanhaa Guest

    ASM wrote:
    > tanhaa a écrit :
    > > Hi all,
    > >
    > > Just curious if Javascript can do this for me:
    > >
    > > I have data stored in a particular format and when displaying that
    > > data, I want it to be shown in specific format.

    >
    > On what moment do you want this ?
    > - during loading ?
    > - after loading ?
    > - for one or several numbers ?
    >
    > > the numbers are stored in a string format: 3335551212
    > >
    > > When the number is displayed, I want it to be displayed as:
    > > (333) 555-1212

    >
    > In header of your page :
    >
    > <script type="text/javascript">
    >
    > function toPhone(m) {
    > m = '('+m.substring(0,3)+') '+
    > m.substring(3,6)+'-'+
    > m.substring(6);
    > return m;
    > }
    >
    > // example to delete after test :
    > alert(toPhone('3335551212'));
    >
    > </script>
    >
    > > I only want it to be displayed like that.

    >
    > so, OK no verification on this data
    >
    > > All my search on the web to see if this is possible has been really
    > > fruitless. Can anyone please help?

    >
    > Phone number formatted during loading,
    > add where you want it :
    >
    > <script type="text/javascript">
    > document.write(toPhone('3335551212'));
    > </script>
    >
    > --
    > ASM


    Thank you ASM
    and Thank you Randy... your codes did the job. I really appreciate the
    help guys!!

    Regards,


    Amit Malhotra
     
    tanhaa, Sep 22, 2006
    #4
  5. tanhaa

    tanhaa Guest

    tanhaa wrote:
    > ASM wrote:
    > > tanhaa a écrit :
    > > > Hi all,
    > > >
    > > > Just curious if Javascript can do this for me:
    > > >
    > > > I have data stored in a particular format and when displaying that
    > > > data, I want it to be shown in specific format.

    > >
    > > On what moment do you want this ?
    > > - during loading ?
    > > - after loading ?
    > > - for one or several numbers ?
    > >


    Just to answer the questions:)

    I wanted it done during loading.

    for several numbers, but the numbers are stored as a variable, so I'm
    passing that variable through the javascript function.


    > > Phone number formatted during loading,
    > > add where you want it :
    > >
    > > <script type="text/javascript">
    > > document.write(toPhone('3335551212'));
    > > </script>
    > >


    now, the script does exactly what I want it to do, thank you for that.
    However, what if some guy saves the number as 333.555-1212. Is there
    any way for Javascript to take out the non-numerical characters and
    turn it into a string of 3335551212 and then apply the format of (333)
    555-1212?

    Not so important for me, I am just curious for my own knowledge.

    I know php can do that by use ereg() function i believe.

    TIA.

    Regards,

    Amit Malhotra
     
    tanhaa, Sep 22, 2006
    #5
  6. tanhaa

    ASM Guest

    tanhaa a écrit :
    > Just to answer the questions:)
    >
    > I wanted it done during loading.


    Good :)


    > now, the script does exactly what I want it to do, thank you for that.
    > However, what if some guy saves the number as 333.555-1212. Is there
    > any way for Javascript to take out the non-numerical characters and
    > turn it into a string of 3335551212 and then apply the format of (333)
    > 555-1212?
    >
    > Not so important for me, I am just curious for my own knowledge.
    >
    > I know php can do that by use ereg() function i believe.


    Same regexpr exist in JS but they aren't my speciality :-(

    In a very laborious way :

    function fromPhone(p) {
    if(p.indexOf('.')>0) p.replace('.','');
    if(p.indexOf('-')>0) p.replace('-','');
    if(p.indexOf(' ')>0) p.replace(' ','');
    return p;
    }

    --
    ASM
     
    ASM, Sep 23, 2006
    #6
  7. JRS: In article <>,
    dated Fri, 22 Sep 2006 14:34:25 remote, seen in news:comp.lang.javascript,
    tanhaa <> posted :
    >
    >now, the script does exactly what I want it to do, thank you for that.
    >However, what if some guy saves the number as 333.555-1212. Is there
    >any way for Javascript to take out the non-numerical characters and
    >turn it into a string of 3335551212 and then apply the format of (333)
    >555-1212?


    Methods using substring and concatenation are tediously elementary and long-
    winded for this; use RegExp substitutions.

    var A = "333.555-1212" // or other means
    var B = A.replace(/\D/g, "") // to 3335551212
    var C = B.replace(/(...)(...)(....)$/, "($1) $2-$3") // to (333) 555-1212

    The second substitution expects 10 decimal digits but will tolerate other
    numbers.

    You should, however, unless restricting yourself to phones using US-type
    numbering, allow for other formats. I believe that JL can be telephoned,
    from within the USA, using something like 0044 #### ######.

    The above can be adapted to insert a leading 1 for 10-digit numbers.

    See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

    It's a good idea to read the newsgroup and its FAQ.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 23, 2006
    #7
  8. tanhaa

    tanhaa Guest


    >
    > The above can be adapted to insert a leading 1 for 10-digit numbers.
    >
    > See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
    >
    > It's a good idea to read the newsgroup and its FAQ.
    > --
    > © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    > <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.



    Thank you for all the help guys. It was much appreciated.


    Regards,


    Amit Malhotra
     
    tanhaa, Sep 23, 2006
    #8
  9. JRS: In article <45146d45$0$5073$>, dated Sat, 23
    Sep 2006 01:09:58 remote, seen in news:comp.lang.javascript, ASM
    <> posted :

    >In a very laborious way :


    C'est comme ça.

    >function fromPhone(p) {
    >if(p.indexOf('.')>0) p.replace('.','');
    >if(p.indexOf('-')>0) p.replace('-','');
    >if(p.indexOf(' ')>0) p.replace(' ','');
    >return p;
    >}


    The tests are not needed; if a RegExp replace finds nothing to replace,
    it returns the string unchanged. But without a Global flag, only one
    replacement occurs. Also, p is unchanged; you need p = p.replace...

    You could have written

    function fromPhone(p) { return p.replace(/[ .-]/g, '') }

    to remove all of dot dash space; but using /\D/g will replace all non-
    digits.

    You can use A LOCAL COPY of parts of
    <URL:http://www.merlyn.demon.co.uk/js-valid.htm>
    <URL:http://www.merlyn.demon.co.uk/js-quick.htm>
    for testing.

    Except for those doing coursework who have been taught .substring but
    not yet RegExp, RegExps should be considered for all string-editing;
    they are almost always better than decomposition and re-assembly, at
    least where the latter uses constant numerical arguments for .substring.

    <FAQENTRY> It's a pity that the FAQ is so weak on RegExps, but study
    section 4.16.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 23, 2006
    #9
  10. tanhaa

    Randy Webb Guest

    Dr John Stockton said the following on 9/23/2006 5:23 PM:

    <snip>

    > <FAQ***RY> It's a pity that the FAQ is so weak on RegExps, but study
    > section 4.16.


    Where is your draft proposal for an entry on RegExps?

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Sep 25, 2006
    #10
  11. JRS: In article <>, dated
    Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
    Randy Webb <> posted :
    >Dr John Stockton said the following on 9/23/2006 5:23 PM:
    >
    ><snip>
    >
    >> <FAQ***RY> It's a pity that the FAQ is so weak on RegExps, but study
    >> section 4.16.

    >
    >Where is your draft proposal for an entry on RegExps?


    One can be available shortly after I get a positive indication from the
    current FAQ maintainer that posting such a draft will lead to a FAQ
    entry on the subject being published, after discussion, in a timely
    manner. OTOH, IIRC, I've made enough <FAQENTRY>s on the subject of
    RegExps to give a start.

    Read <URL:http://www.merlyn.demon.co.uk/js-valid.htm> for a selection of
    examples and pieces of text that might be used as a starting-point. For
    example, the first three sentences of #FOR on that page might be
    condensed as an initial statement of capability.

    The page has a Links section. I don't recall link-checking it recently,
    so they may not all be valid; and I'd be pleased to hear of any better
    ones (provided that the page does not kill my browser).

    As far as I know, there's no FAQ Note on RegExps or validation; some of
    the material in js-valid.htm could be put into a Note.

    But, for the FAQ, the most important thing is to get RegExp into the
    Subject of a Section 4 entry (presently it occurs nowhere in the text
    proper), and at least one additional link to something more discursive
    than a MAN page.

    Renaming
    4.16 How do I trim whitespace - LTRIM/RTRIM/TRIM?
    to
    4.16 How can a RegExp trim whitespace - LTRIM/RTRIM/TRIM?
    would be a start (the non-RegExp version can IMHO now be removed).

    Moreover, in spite of the number of questions on validation, the FAQ
    does not contain "valid".

    Where is *your* material on RegExps?


    It's a good idea to read the newsgroup and its FAQ.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 25, 2006
    #11
  12. tanhaa

    Randy Webb Guest

    Dr John Stockton said the following on 9/25/2006 3:40 PM:
    > JRS: In article <>, dated
    > Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
    > Randy Webb <> posted :
    >> Dr John Stockton said the following on 9/23/2006 5:23 PM:
    >>
    >> <snip>
    >>
    >>> <FAQ***RY> It's a pity that the FAQ is so weak on RegExps, but study
    >>> section 4.16.

    >> Where is your draft proposal for an entry on RegExps?

    >
    > One can be available shortly after I get a positive indication from the
    > current FAQ maintainer that posting such a draft will lead to a FAQ
    > entry on the subject being published, after discussion, in a timely
    > manner. OTOH, IIRC, I've made enough <FAQENTRY>s on the subject of
    > RegExps to give a start.


    Then make one, and put it in a page. If nothing else, it could be
    referred to in signatures and other documents. It doesn't have to be in
    the FAQ itself as much as created.

    > The page has a Links section. I don't recall link-checking it recently,
    > so they may not all be valid; and I'd be pleased to hear of any better
    > ones (provided that the page does not kill my browser).


    The links to developer.netscape.com are non-existent anymore (They go to
    404 pages). The link to MSDN has been moved to:

    <URL:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/ab0766e1-7037-45ed-aa23-706f58358c0e.asp>

    > But, for the FAQ, the most important thing is to get RegExp into the
    > Subject of a Section 4 entry (presently it occurs nowhere in the text
    > proper), and at least one additional link to something more discursive
    > than a MAN page.


    Totally agreed, but that isn't up to me :-\

    <snip>

    > Where is *your* material on RegExps?


    In a 450 page book sitting on my desk.
    But alas, I am not the one complaining about the lack of an entry on
    RegExps.
    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Sep 26, 2006
    #12
  13. JRS: In article <>, dated
    Tue, 26 Sep 2006 03:09:53 remote, seen in news:comp.lang.javascript,
    Randy Webb <> posted :
    >Dr John Stockton said the following on 9/25/2006 3:40 PM:
    >> JRS: In article <>, dated
    >> Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
    >> Randy Webb <> posted :
    >>> Dr John Stockton said the following on 9/23/2006 5:23 PM:
    >>>
    >>> <snip>
    >>>
    >>>> <FAQ***RY> It's a pity that the FAQ is so weak on RegExps, but study
    >>>> section 4.16.
    >>> Where is your draft proposal for an entry on RegExps?

    >>
    >> One can be available shortly after I get a positive indication from the
    >> current FAQ maintainer that posting such a draft will lead to a FAQ
    >> entry on the subject being published, after discussion, in a timely
    >> manner. OTOH, IIRC, I've made enough <FAQENTRY>s on the subject of
    >> RegExps to give a start.

    >
    >Then make one, and put it in a page. If nothing else, it could be
    >referred to in signatures and other documents. It doesn't have to be in
    >the FAQ itself as much as created.


    A draft FAQ entry needs to be rather short; it should just indicate the
    applications of RegExps, give about 3 examples (simple, moderate, &
    potent complexity), and link to good MAN-page and discursive Web sites
    and maybe a FAQ Note. There's no sense in trying to explain as briefly
    as that in a Web page on the topic. I have written such a page, though
    it's weak on non-greedy and worse on look-ahead.


    >> The page has a Links section. I don't recall link-checking it recently,
    >> so they may not all be valid; and I'd be pleased to hear of any better
    >> ones (provided that the page does not kill my browser).

    >
    >The links to developer.netscape.com are non-existent anymore (They go to
    >404 pages).


    Agreed; but, as IIRC they were good pages, I kept the links and added
    the reference to the Wayback machine.

    > The link to MSDN has been moved to:
    >
    ><URL:
    >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/a
    >b0766e1-7037-45ed-aa23-706f58358c0e.asp>


    Ta; changed. An identifier of a mere 80 decimal digits is capable,
    rather approximately, of individually addressing every atom in the
    observable Universe; that URL (effectively 75 base-36 characters, plus)
    is TOO LONG.

    >But alas, I am not the one complaining about the lack of an entry on
    >RegExps.


    Only about the lack of a draft entry.

    --
    © John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 4 ©
    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
     
    Dr John Stockton, Sep 26, 2006
    #13
    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. Scott Allen

    Re: Exception not being displayed

    Scott Allen, Sep 21, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    402
    Scott Allen
    Sep 22, 2004
  2. =?Utf-8?B?TWFydHkgVS4=?=

    Exception not being displayed

    =?Utf-8?B?TWFydHkgVS4=?=, Sep 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    391
    =?Utf-8?B?TWFydHkgVS4=?=
    Sep 21, 2004
  3. Replies:
    6
    Views:
    660
  4. Walter Gildersleeve

    problem with GUI hanging before being displayed

    Walter Gildersleeve, Oct 2, 2003, in forum: Java
    Replies:
    0
    Views:
    2,053
    Walter Gildersleeve
    Oct 2, 2003
  5. boney
    Replies:
    1
    Views:
    568
Loading...

Share This Page