Formatting data being displayed

T

tanhaa

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
 
R

Randy Webb

tanhaa said the following on 9/22/2006 3:29 PM:
Hi all,

Just curious if Javascript can do this for me:

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.
 
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'));

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

tanhaa

ASM said:
tanhaa a écrit :

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


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'));



so, OK no verification on this data


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

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

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

Regards,


Amit Malhotra
 
T

tanhaa

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.


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
 
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;
}
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 22 Sep 2006 14:34:25 remote, seen in
tanhaa said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 23
Sep 2006 01:09:58 remote, seen in ASM
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.
 
R

Randy Webb

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

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

Dr John Stockton

JRS: In article <[email protected]>, dated
Mon, 25 Sep 2006 02:34:12 remote, seen in
Randy Webb said:
Dr John Stockton said the following on 9/23/2006 5:23 PM:



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

Randy Webb

Dr John Stockton said the following on 9/25/2006 3:40 PM:
JRS: In article <[email protected]>, dated
Mon, 25 Sep 2006 02:34:12 remote, seen in

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/d...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 :-\

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

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 26 Sep 2006 03:09:53 remote, seen in
Randy Webb said:
Dr John Stockton said the following on 9/25/2006 3:40 PM:

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top