<div>本文转自:http://www.cnblogs.com/ly5201314/archive/2009/03/04/1402993.html<br><p>javascript:只能输入数字(ie、ff)</p><p>为了解决只能输入数字的问题,网上有许多资料,现归拢一下。</p><p><strong> 一、不带负号的输入</strong></p><p>这里,没有解决“正负号”的问题。</p><p>由于“正负号”必须出现在数字的最前端,因此,必须要判断当前光标所在的位置是否在输入文本框的首位。</p><table style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; background-color: #e0e0e0;" cellspacing="0" cellpadding="0" width="100%" border="1"><tr><td><p><script language="javascript" type="text/javascript" ><br><!--<br>//调用方式:onkeydown = "digitinput(this,event);"<br>function digitinput(el,ev) {<br>//8:退格键、46:delete、37-40: 方向键<br>//48-57:小键盘区的数字、96-105:主键盘区的数字<br>//110、190:小键盘区和主键盘区的小数<br>//189、109:小键盘区和主键盘区的负号</p><p> var event = ev || window.event; //ie、ff下获取事件对象<br> var currentkey = event.charcode||event.keycode; //ie、ff下获取键盘码<br><br> //小数点处理<br> if (currentkey == 110 || currentkey == 190) {<br> if (el.value.indexof(".")>=0) <br> if (window.event) //ie<br> event.returnvalue=false; //e.returnvalue = false;效果相同.<br> else //firefox<br> event.preventdefault();</p><p> } else <br> if (currentkey!=8 && currentkey != 46 && (currentkey<37 || currentkey>40) && (currentkey<48 || currentkey>57) && (currentkey<96 || currentkey>105))<br> if (window.event) //ie<br> event.returnvalue=false; //e.returnvalue = false;效果相同.<br> else //firefox<br> event.preventdefault();</p><p>}<br>--></p><p></script></p></td></tr></table><p><strong> 二、带负号的输入</strong></p><p> 判断带负号的输入,则必须要知道光标的位置,也就是说,只有当光标在最前端时,负号才有效。</p><table style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; background-color: #e0e0e0;" cellspacing="0" cellpadding="0" width="100%" border="1"><tr><td><p><script language="javascript" type="text/javascript" ><br><!--<br>//调用方式:onkeydown = "digitinput(this,event);"<br>function digitinput(el,ev) {<br>//8:退格键、46:delete、37-40: 方向键<br>//48-57:小键盘区的数字、96-105:主键盘区的数字<br>//110、190:小键盘区和主键盘区的小数<br>//189、109:小键盘区和主键盘区的负号</p><p> var event = ev || window.event; //ie、ff下获取事件对象<br> var currentkey = event.charcode||event.keycode; //ie、ff下获取键盘码<br><br> //小数点处理<br> if (currentkey == 110 || currentkey == 190) {<br> if (el.value.indexof(".")>=0) <br> if (window.event) //ie<br> event.returnvalue=false; //e.returnvalue = false;效果相同.<br> else //firefox<br> event.preventdefault();</p><p> } else<br> //负号处理<br><strong> if (currentkey == 189 || currentkey == 109) {<br> if (getposition(el)>0 || el.value.indexof("-")>=0)<br></strong> if (window.event) //ie<br> event.returnvalue=false; //e.returnvalue = false;效果相同.<br> else //firefox<br> event.preventdefault();<br> } else<br> if (currentkey!=8 && currentkey != 46 && (currentkey<37 || currentkey>40) && (currentkey<48 || currentkey>57) && (currentkey<96 || currentkey>105))<br> if (window.event) //ie<br> event.returnvalue=false; //e.returnvalue = false;效果相同.<br> else //firefox<br> event.preventdefault();</p><p>}<br>/**<br> * 获取光标所在的字符位置<br> * @param obj 要处理的控件, 支持文本域和输入框<br> * @author hotleave<br> */<br>function getposition(obj){<br> var result = 0;<br> if(obj.selectionstart){ //非ie浏览器<br> result = obj.selectionstart<br> }else{ //ie<br> var rng;<br> if(obj.tagname == "textarea"){ //如果是文本域<br> rng = event.srcelement.createtextrange();<br> rng.movetopoint(event.x,event.y);<br> }else{ //输入框<br> rng = document.selection.createrange();<br> }<br> rng.movestart("character",-event.srcelement.value.length);<br> result = rng.text.length;<br> }<br> return result;<br>}</p><p>--></p><p></script></p></td></tr></table><p>getposition(obj)函数,是一个获取光标位置的通用函数。</p><p>为了减少传入的参数,事件对象是可以通过事件获取的:</p><table style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; background-color: #ccffff;" cellspacing="0" cellpadding="0" width="100%" border="1"><tr><td>var el = window.event.srcelement||ev.target;</td></tr></table><p>只需将传digitinput(el,ev)的第一句加上上面这条语句,并且将传入参数只有ev一个即可,这样,减少了传入值。</p><p><strong> 三、扩展</strong></p><p> 我们可以增加一个传入的参数值,用于限定允许输入的数字条件:</p><p>1:允许正整数;2、允许正小数;3、允许负整数;4、允许负小数。</p><p>当然,传入一个正则表达式也可以实现。</p><div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;"><img alt="" src="http://www.cnblogs.com/images/outliningindicators/none.gif" align="top"><span style="color: #000000;">functiononlynum()<br><img id="codehighlighter1_20_477_open_image" src="http://www.cnblogs.com/images/outliningindicators/expandedblockstart.gif" align="top"><img id="codehighlighter1_20_477_closed_image" style="display: none;" src="http://www.cnblogs.com/images/outliningindicators/contractedblock.gif" align="top"></span><span id="codehighlighter1_20_477_open_text"><span style="color: #000000;">{<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #000000;">!</span><span style="color: #000000;">((</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">>=</span><span style="color: #000000;">48</span><span style="color: #000000;">&&</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;"><=</span><span style="color: #000000;">57</span><span style="color: #000000;">)<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">>=</span><span style="color: #000000;">96</span><span style="color: #000000;">&&</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;"><=</span><span style="color: #000000;">105</span><span style="color: #000000;">)<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">8</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">退格</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">46</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">del</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">27</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">esc</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">37</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">左</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">39</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">右</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">16</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">shift</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #000000;">||</span><span style="color: #000000;">(</span><span style="color: #0000ff;">event</span><span style="color: #000000;">.keycode</span><span style="color: #000000;">==</span><span style="color: #000000;">9</span><span style="color: #000000;">)</span><span style="color: #008000;">//</span><span style="color: #008000;">tab</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;">)<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top">)<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">考虑小键盘上的数字键</span><span style="color: #008000;"><br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/inblock.gif" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">event</span><span style="color: #000000;">.returnvalue</span><span style="color: #000000;">=</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br><img alt="" src="http://www.cnblogs.com/images/outliningindicators/expandedblockend.gif" align="top">}</span></span><span style="color: #000000;"> <br></span>
[转]javascript:只能输入数字(ie、ff)
最新推荐文章于 2019-03-20 20:39:51 发布