相忘于江湖

不抛弃,不放弃... 请给我勇敢,改变可以改变的;请给我坚强,接受不可以改变的;请给我智慧,分辨这两者。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


<!-- 要实现此效果需要 1 个步骤: -->

<!-- 第 1 步: -->
<!-- 把下面的代码加到<BODY></BODY>区域中: -->

<script type='text/javascript'>
// [dFilter] - A Numerical Input Mask for JavaScript
// Written By Dwayne Forehand - March 27th, 2003
// Please reuse & redistribute while keeping this notice.

var dFilterStep

function dFilterStrip (dFilterTemp, dFilterMask)
{
dFilterMask = replace(dFilterMask,'#','');
for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++)
{
dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
}
return dFilterTemp;
}

function dFilterMax (dFilterMask)
{
dFilterTemp = dFilterMask;
for (dFilterStep = 0; dFilterStep < (dFilterMask.length+1); dFilterStep++)
{
if (dFilterMask.charAt(dFilterStep)!='#')
{
dFilterTemp = replace(dFilterTemp,dFilterMask.charAt(dFilterStep),'');
}
}
return dFilterTemp.length;
}

function dFilter (key, textbox, dFilterMask)
{
dFilterNum = dFilterStrip(textbox.value, dFilterMask);

if (key==9)
{
return true;
}
else if (key==8&&dFilterNum.length!=0)
{
dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
}
else if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) )
{
dFilterNum=dFilterNum+String.fromCharCode(key);
}

var dFilterFinal='';
for (dFilterStep = 0; dFilterStep < dFilterMask.length; dFilterStep++)
{
if (dFilterMask.charAt(dFilterStep)=='#')
{
if (dFilterNum.length!=0)
{
dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
dFilterNum = dFilterNum.substring(1,dFilterNum.length);
}
else
{
dFilterFinal = dFilterFinal + "";
}
}
else if (dFilterMask.charAt(dFilterStep)!='#')
{
dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep);
}
// dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
}


textbox.value = dFilterFinal;
return false;
}

function replace(fullString,text,by) {
// Replaces text with by in string
var strLength = fullString.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return fullString;

var i = fullString.indexOf(text);
if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
if (i == -1) return fullString;

var newstr = fullString.substring(0,i) + by;

if (i+txtLength < strLength)
newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

return newstr;
}

</script>
<form name="fred" action="fred.htm" method="post">
<table>
<tr>
<td>输入数字:</td>
<td><input value="" type="text" onKeyDown="javascript:return dFilter (event.keyCode, this, '###-##-####');" style="font-family:verdana;font-size:10pt;width:110px;"></td>
</tr>
<tr>
<td>电话号码:</td>
<td><input value="" type="text" onKeyDown="javascript:return dFilter (event.keyCode, this, '(###) ###-####');" style="font-family:verdana;font-size:10pt;width:110px;"></td>
</tr>
<tr>
<td>邮政编码:</td>
<td><input value="" type="text" onKeyDown="javascript:return dFilter (event.keyCode, this, '###-###');" style="font-family:verdana;font-size:10pt;width:110px;"></td>
</tr>
</table>
</form>
posted on 2010-07-17 10:09  playman0211  阅读(362)  评论(0编辑  收藏  举报