By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Formatting data being displayed

P: n/a
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

Sep 22 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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/
Sep 22 '06 #2

P: n/a
ASM
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
Sep 22 '06 #3

P: n/a

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

Sep 22 '06 #4

P: n/a

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

Sep 22 '06 #5

P: n/a
ASM
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
Sep 22 '06 #6

P: n/a
JRS: In article <11**********************@m73g2000cwd.googlegroups .com>,
dated Fri, 22 Sep 2006 14:34:25 remote, seen in news:comp.lang.javascript,
tanhaa <sa***********@gmail.composted :
>
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 23 '06 #7

P: n/a
>
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.htmjscr 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

Sep 23 '06 #8

P: n/a
JRS: In article <45**********************@news.orange.fr>, dated Sat, 23
Sep 2006 01:09:58 remote, seen in news:comp.lang.javascript, ASM
<st*********************@wanadoo.fr.invalidposte d :
>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.

<FAQENTRYIt'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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 23 '06 #9

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

<snip>
<FAQ***RYIt'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/
Sep 25 '06 #10

P: n/a
JRS: In article <59******************************@comcast.com>, dated
Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>Dr John Stockton said the following on 9/23/2006 5:23 PM:

<snip>
><FAQ***RYIt'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.htmfor 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 25 '06 #11

P: n/a
Dr John Stockton said the following on 9/25/2006 3:40 PM:
JRS: In article <59******************************@comcast.com>, dated
Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>Dr John Stockton said the following on 9/23/2006 5:23 PM:

<snip>
>><FAQ***RYIt'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/
Sep 26 '06 #12

P: n/a
JRS: In article <dq******************************@comcast.com>, dated
Tue, 26 Sep 2006 03:09:53 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>Dr John Stockton said the following on 9/25/2006 3:40 PM:
>JRS: In article <59******************************@comcast.com>, dated
Mon, 25 Sep 2006 02:34:12 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.composted :
>>Dr John Stockton said the following on 9/23/2006 5:23 PM:

<snip>

<FAQ***RYIt'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/de...cript56/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.
Sep 26 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.