Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Formatting data being displayed

Reply
Thread Tools

Formatting data being displayed

 
 
tanhaa
Guest
Posts: n/a
 
      09-22-2006
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

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      09-22-2006
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/
 
Reply With Quote
 
 
 
 
ASM
Guest
Posts: n/a
 
      09-22-2006
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
 
Reply With Quote
 
tanhaa
Guest
Posts: n/a
 
      09-22-2006

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

 
Reply With Quote
 
tanhaa
Guest
Posts: n/a
 
      09-22-2006

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

 
Reply With Quote
 
ASM
Guest
Posts: n/a
 
      09-22-2006
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-23-2006
JRS: In article < .com>,
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.
 
Reply With Quote
 
tanhaa
Guest
Posts: n/a
 
      09-23-2006

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

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-23-2006
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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      09-25-2006
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/
 
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
Japanese Text not displayed on Image Generated by Servlet on winXP, Linux but displayed on Win2000 boney Java 1 12-15-2006 02:24 PM
IE's view source not showing what is actually being displayed jwwishart@gmail.com ASP .Net 6 02-14-2006 04:23 AM
Re: Exception not being displayed Scott Allen ASP .Net 5 09-22-2004 01:20 PM
Exception not being displayed =?Utf-8?B?TWFydHkgVS4=?= ASP .Net 0 09-21-2004 06:49 PM
Carriage returns/formatting not displayed in output.asp Doc Wally ASP General 1 10-10-2003 05:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57