473,578 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1435
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 formatUSPhoneNu mber(numToForma t){
areaCode = numToFormat.sub string(0,3)
prefix = numToFormat.sub string(3,6)
suffix = numToFormat.sub string(6,10)
return("(" + areaCode + ") " + prefix + "-" + suffix)
}

document.write( formatUSPhoneNu mber('333555121 2'))

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.javas cript 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('333555 1212'));
</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('333555 1212'));
</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('333555 1212'));
</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************ **********@m73g 2000cwd.googleg roups.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.demo n.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.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.demo n.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.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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************ *********@wanad oo.fr.invalidpo sted :
>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.demo n.co.uk/js-valid.htm>
<URL:http://www.merlyn.demo n.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.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1686
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 like to be able to format the numbers being displayed using thousand separators, etc. could someone please point me in the right direction as to how...
8
12029
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 expressions are the same for all controls Under some undetermined circumstances, when such subform is displayed, the controls on the subform start...
2
2955
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 showing or only part of the data in the field displays (i.e. it will display the last name but not the first name or it will display neither). When...
4
2069
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 . The FK in the personnel table is also Long Integer, and formatted as the original field with zero's. Data Example: Original: 5, 9, 15, 99,...
12
2342
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 access database.
8
1691
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 displayed inside SQL ie I cannot enter new blank lines I have tried using lots of spaces but this does not work when I use the website to display...
12
5933
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> </ItemTemplate>
7
16390
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 they can only input numerical values. Is there any way to do this? Currently my formatting works but if the user enters a value manually the...
5
1237
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 § Regex pattern? o If the number of digits in the EmployeeNumber is less than 6
0
7847
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8290
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5342
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.