格式化正在显示的数据 [英] Formatting data being displayed

查看:67
本文介绍了格式化正在显示的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


只是好奇Javascript是否可以为我这样做:


我有以特定格式存储数据的时间显示

数据,我希望它以特定格式显示。


EG电话号码:


号码以字符串格式存储:3335551212

显示号码时,我希望它显示为:(333)

555-1212

我知道有几种方法可以确保验证是否已经完成了b
$ b正在插入数据,以便可以以(333)555-1212格式存储数字

,但我不希望数据存储像

那样,我只希望它像这样显示。


我在网上搜索的所有内容都是真的

无果而终。有人可以帮忙吗?


Amit Malhotra

解决方案

tanhaa在9上说了以下内容/ 22/2006 3:29 PM:


大家好,


只是好奇Javascript是否可以为我这样做:



< snip>


数字以字符串格式存储:3335551212


显示数字时,我希望它显示为:(333)

555-1212



函数formatUSPhoneNumber(numToFormat){

areaCode = numToFormat.substring(0,3)

prefix = numToFormat.substring(3,6)

后缀= numToFormat.substring(6,10)

return("(" + areaCode +")" + prefix +" - " + suffix)

}


document.write(formatUSPhoneNumber('''3335551212'))


输出:


(333)555-121 2


注意后的空格)。如果你不想要它,请在功能的

返回行中删除它。

-

Randy
机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


tanhaaaécrit:


大家好,


只是好奇Javascript是否能为我这样做:


我以特定格式存储数据并在显示时显示

数据,我希望它以特定的格式显示。



在什么时候你想要这个?

- 在装货期间?

- 装货后?

- 一个或多个数字?


数字以字符串格式存储:3335551212


当显示数字时,我希望它显示为:

(333)555-1212



在页面的标题中:


< script type =" text / javascript">


函数toPhone(m){

m =''(''+ m.substring(0,3)+'')''+

m.substring(3,6)+'' - ''+

m.substring(6);

返回m;

}


//删除后的示例测试:

alert(toPhone(''3335551212''));


< / script>


我只想让它显示出来。



所以,确定没有关于此数据的验证


我在网上的所有搜索,看看是否这个可能已经真的很无果实了。有人可以帮忙吗?



加载时格式化的电话号码,

添加到你想要的地方:


< script type =" text / javascript">

document.write(toPhone(''3335551212''));

< / script>


-

ASM




ASM写道:
< blockquote class =post_quotes>
tanhaaaécrit:


大家好,


好​​奇如果Javascript可以为我做这个:


我有以特定格式存储的数据,当显示

数据时,我希望它以特定格式显示。



在什么时候你想要这个?

- 在装货期间?

- 装货后?

- 一个或多个数字?


数字以字符串格式存储:3335551212


当显示数字时,我希望它显示为:

(333)555-1212



在页面的标题中:


< script type =" text / javascript">


函数toPhone(m){

m =''(''+ m.substring(0,3)+'')''+

m.substring(3,6)+'' - ''+

m.substring(6);

返回m;

}


//删除后的示例测试:

alert(toPhone(''3335551212''));


< / script>


我只想让它显示出来。



所以,确定没有关于此数据的验证


我在网上的所有搜索,看看是否这个可能已经真的很无果实了。有人可以帮忙吗?



加载时格式化的电话号码,

添加到你想要的地方:


< script type =" text / javascript">

document.write(toPhone(''3335551212''));

< / script>


-

ASM



谢谢ASM

谢谢兰迪......你的代码完成了这项工作。我真的很感激

帮助家伙!!


问候,

Amit Malhotra


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

解决方案

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/


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



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


这篇关于格式化正在显示的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆