473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Formatting data being displayed

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

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

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
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
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
>
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Saix News | last post by:
hi, i have a dataset and a number of controls that are bound to it. some of the records being displayed are numerical and are bound to text boxes. they display without a problem but i would...
8
by: Dimitri Furman | last post by:
Given: Access 2002/2003 A subform in datasheet or continuous view, placed on a tab page (this last may or may not matter) Conditional formatting applied to some controls on the subform - format...
2
by: Von Bailey | last post by:
I have a form where the conditional formatting is set on some fields to bold if certain conditions are met. However, when the conditions are met some of the data that is to bold is either not...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
12
by: dmieluk | last post by:
Problem: When moving between records, I need to control which combo/text boxes are displayed on the current form, dependent upon data in the current record. More: I building my first...
8
by: alamb200 | last post by:
Hi I have set up a SQL database to contain alist FAQ's for our company and then plan to pull this info off using a web page. So far I have entered the data but I am unable to control how it is...
12
by: tarscher | last post by:
Hi all, A simple problem that seems hard to solve... I have a gridview with a date row. <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("time") %>'></asp:Label>...
7
by: Ryan | last post by:
I have a DataGridView which displays numeric (Int32) data from an underlying database. I want the numbers to be displayed in numeric format "#,###" (with commas). I want to also limit the user so...
5
by: Berryl Hesh | last post by:
Requirements for a field that represents an EmployeeNumber: · The field is always 6 characters long, and is stored as a 'string' in the db o All characters must be digits § ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.