想要让input里只输入数字,或者字母,汉字之类的,一般是把onkeyup里加上表达式。
往每个标签里添加动作实在是太麻烦,于是花了2个小时做了一件一劳永逸的事情:
下次用这个函数就能搞定input输入框的输入限制了
- <?php
- //power by Jack 2010-1-2 15:05 with his GF
- function only($input_typeid){
- if ($input_typeid==1)//只能输入数字
- {
- echo "onkeyup=\"this.value=this.value.replace(/\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\D/g,'')\"";
- }
- else if ($input_typeid==2)//只能输入数字和小数点
- {
- echo "onkeyup=\"if(isNaN(value))execCommand('undo')\" onafterpaste=\"if(isNaN(value))execCommand('undo')\"";
- }
- else if ($input_typeid==3)//只能输入字母和汉字
- {
- echo "onkeyup=\"value=value.replace(/[\d]/g,'') \"onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))\"";
- }
- else if ($input_typeid==4)//只能输入英文字母和数字,不能输入中文
- {
- echo "onkeyup=\"value=value.replace(/[^\w\.\/]/ig,'')\"";
- }
- else if ($input_typeid==5)//只能输入数字和英文
- {
- echo "onKeyUp=\"value=value.replace(/[^\d|chun]/g,'')\"";
- }
- else if ($input_typeid==6)//小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
- {
- echo "onKeyPress=\"if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false\"";
- }
- else if ($input_typeid==7)//小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号
- {
- echo "onkeyup=\"this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')\"";
- }
- }
- ?> 使用的时候非常简单,在input里调用这个函数即可,我是给大家做演示,所以乱调的哦,用的时候按需要调用即可
- 价格<input type="text" name="permoney" maxlength="20" value="" <?php only(1)?>/>金币
- 时间<input type="text" name="time1" maxlength="20" value="" <?php only(2)?>/>小时
- 产量<input type="text" name="forin" maxlength="20" value="" <?php only(3)?>/>个
- 售价<input type="text" name="sell" maxlength="20" value="" <?php only(4)?>/>金币
- 经验<input type="text" name="exp" maxlength="20" value="" <?php only(5)?>/>
- 用途<input type="text" name="useful" maxlength="20" value="" <?php only(6)?>/>
- 小贴士<input type="text" name="tip" maxlength="20" value=""<?php only(7)?>/>

|