格式化数字Easily format as many numeric
fields as you need, and specify both long and
short formats. |
|
|
Social Security # |
ONKEYPRESS call: FormatNum(this, "###-##-#### ") |
|
Phone |
ONKEYPRESS call: FormatNum(this, "(###) ###-#### ", "###-#### ") |
|
Default(Phone) |
ONKEYPRESS call: FormatNum(this) |
本脚本可以把数字格式化成你想要的字符串格式,调用方法非常简单
查看演示页面 View Demo 查看全部代码 View Code
在网页<head>区添加以下代码
<SCRIPT LANGUAGE="JavaScript">
<!-- JavaScript
function FormatNumber(num, format, shortformat)
{
if(format==null)
{
// Choose the default format you prefer for the <b style="color:black;background-
color:#a0ffff">number</b>.
//format = "#-(###) ###-#### "; // Telephone w/ LD Prefix and Area Code
format = "(###) ###-#### "; // Telephone w/ Area Code
//format = "###-###-####"; // Telephone w/ Area Code (dash seperated)
//format = "###-##-####"; //Social Security <b
style="color:black;background-color:#a0ffff">Number</b>
}
if(shortformat==null)
{
// Choose the short format (without area code) you prefer.
//If you do not want multiple formats, leave it as "".
//var shortformat = "###-#### ";
var shortformat = "";
}
//----------------------------------------------------------------------------------------------------------
-----------
//----------------------------------------This code can be used to format any <b
style="color:black;background-color:#a0ffff">number</b>. ---------------------------------
//----------------------------------------Simply change the format to a <b style="color:black;background-
color:#a0ffff">number</b> format ---------------------------------
//---------------------------------------- you prefer. It will ignore all characters ----------------------
-----------
//---------------------------------------- except the #, where it will replace with ----------------------
-----------
//---------------------------------------- user input. ----------------------
-----------
//----------------------------------------------------------------------------------------------------------
-----------
var validchars = "0123456789";
var tempstring = "";
var returnstring = "";
var extension = "";
var tempstringpointer = 0;
var returnstringpointer = 0;
count = 0;
// Get the length so we can go through and remove all non-numeric characters
var length = num.value.length;
// We are only concerned with the format of the phone <b style="color:black;background-
color:#a0ffff">number</b> - extensions can be left alone.
if (length > format.length)
{
length = format.length;
};
// scroll through what the user has typed
for (var x=0; x<length; x++)
{
if (validchars.indexOf(num.value.charAt(x))!=-1)
{
tempstring = tempstring + num.value.charAt(x);
};
};
// We should now have just the #'s - extract the extension if needed
if (num.value.length > format.length)
{
length = format.length;
extension = num.value.substr(format.length, (num.value.length-format.length));
};
// if we have fewer characters than our short format, we'll default to the short version.
for (x=0; x<shortformat.length;x++)
{
if (shortformat.substr(x, 1)=="#")
{
count++;
};
}
if (tempstring.length <= count)
{
format = shortformat;
};
//Loop through the format string and insert the numbers where we find a # sign
for (x=0; x<format.length;x++)
{
if (tempstringpointer <= tempstring.length)
{
if (format.substr(x, 1)=="#")
{
returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
tempstringpointer++;
}else{
returnstring = returnstring + format.substr(x, 1);
}
}
}
// We have gone through the entire format, let's add the extension back on.
returnstring = returnstring + extension;
//we're done - let's return our value to the field.
num.value = returnstring;
}
// - JavaScript - -->
</SCRIPT>
在网页<body>区添加以下代码
<TABLE BORDER="1" WIDTH="100%"> <TR> <TD WIDTH="100%" COLSPAN="2"> <H3 ALIGN="center">格式化数字</H3> <P ALIGN="center">Easily format as many numeric fields as you need, and specify both long and short formats.<BR>Feel free to use this in your scripts. Please leave the byline for others to see. </P> </TD> </TR> <TR> <TD WIDTH="129"> <P ALIGN="right">Social Security #<BR><I><FONT SIZE="1">(Send a custom format)</FONT></I></P> </TD> <TD> <P><INPUT TYPE="text" NAME="first_name" SIZE="25" ONKEYPRESS="FormatNumber (this, '###-##-#### ');"> ONKEYPRESS call: FormatNum(this, "###-##-#### ")</P> </TD> </TR> <TR> <TD WIDTH="129"> <P ALIGN="right">Phone<BR><I><FONT SIZE="1">(Use both long and short formats)</FONT></I></P> </TD> <TD> <P><INPUT TYPE="text" NAME="phone" SIZE="25" ONKEYPRESS="FormatNumber(this, '(###) ###-#### ', '###-#### ');"> ONKEYPRESS call: FormatNum(this, "(###) ###-#### ", "###-#### ")</P> </TD> </TR> <TR> <TD WIDTH="129"> <P ALIGN="right">Default(Phone)<BR><I><FONT SIZE="1">(Settings only use the long format)</FONT></I> </P> </TD> <TD> <P><INPUT TYPE="text" NAME="email" SIZE="25" ONKEYPRESS="FormatNumber (this);"> ONKEYPRESS call: FormatNum(this)</P> </TD> </TR> </table>
通过调用FormatNum函数实现数字格式化




