javascript 限制输入脚本大全


Posted in Javascript onNovember 03, 2009

1.只能输入汉字的
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">

2.只能输入数字的:
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
简易禁止输入汉字
<input type="text" style="ime-mode:disabled">
输入数字和小数点:
onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
javascript 只能输入数字和":".2007-11-24 15:50<input type=text id="aa1" onkeyup="this.value=this.value.replace(/[^\d&:]/g,'')" onblur="this.value=this.value.replace(/[^\d&:]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d&:]/g,'')"/>
只能数字和":",例如在输入时间的时候可以用到。
<input type=text id="aa" onkeyup="value=value.replace(/[^\w&=]|_/ig,'')" onblur="value=value.replace(/[^\w&=]|_/ig,'')" />
只能输入字母和等号,不能输入汉字。
其它的东西:
只能输入数字的脚本javascript- -
1.<input onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')">
上半句意思是键盘键入只能是数字,下半句是粘贴也只能是数字
2.<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能输入数字');this.value='';}">
3.<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
JavaScript限制只能输入数字和英文- -
function isregname( checkobj)
{
var checkOK = "0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var checkStr = checkobj;
var allValid = true;
var decPoints = 0;

for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
return (allValid)
}
----------------
if(!(isregname(obj.loginname.value))){
alert("[会员代号]不符合规范,会员代号只能是英文字母或数字");
obj.loginname.focus();
return(false);
}
if(!(isregname(obj.password.value))){
alert("[密码]不符合规范,密码只能是英文字母或数字");
obj.password.focus();
return(false);
}

4.javascript只能输入英文和数字的输入框
<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
5.可以用Javascript对文本框进行检查,过滤掉非0-9的字符。
<script language="javascript" event="onkeydown" for="document">

if(event.srcElement.name=='TextBox1')

{

if(!KeyIsNumber(event.keyCode))

{

return false;//这句话最关键

}

}

</script>

<script language="javascript">

function KeyIsNumber(KeyCode)

{

//如果输入的字符是在0-9之间,或者是backspace、DEL键

if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46))

{

return true;

}

else

{

return false;

}

}

</script>[url=http://blog.csdn.net/xujh/admin/EditPosts.aspx][/url]

6.限制在文本框里面只能输入IP地址格式
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<style>
.a3{width:30;border:0;text-align:center}
</style>
<script>
function mask(obj){
obj.value=obj.value.replace(/[^\d]/g,'')
key1=event.keyCode
if (key1==37 || key1==39)
{ obj.blur();
nextip=parseInt(obj.name.substr(2,1))
nextip=key1==37?nextip-1:nextip+1;
nextip=nextip>=5?1:nextip
nextip=nextip<=0?4:nextip
eval("ip"+nextip+".focus()")
}
if(obj.value.length>=3)
if(parseInt(obj.value)>=256 || parseInt(obj.value)<=0)
{
alert(parseInt(obj.value)+"IP地址错误!")
obj.value=""
obj.focus()
return false;
}
else
{ obj.blur();
nextip=parseInt(obj.name.substr(2,1))+1
nextip=nextip>=5?1:nextip
nextip=nextip<=0?4:nextip
eval("ip"+nextip+".focus()")
}
}
function mask_c(obj)
{
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
}

</script>
<title>IP地址输入</title>

</head>
<body>IP地址输入
<div style="border-width:1;border-color:balck;border-style:solid;width:165;font-size:9pt">
<input type=text name=ip1 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip2 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip3 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>.
<input type=text name=ip4 maxlength=3 class=a3 onkeyup="mask(this)" onbeforepaste=mask_c()>
</div>
</body>
</HTML>

7.限制输入格式的日期控件


----- 日期控件的另一种思路,限制输入格式的日期控件 ----- 关于日期输入,现在网上的日期控件很多,在这里,我试着模仿了部分CS程序中的日期输入控件,从另一个角度来考虑日期格式的输入,即输入日期时固定格式,用户只能在程序规定的地方输入数据。

例程如下,希望对大家有所帮助~~~ (时间仓促,例程是IE ONLY的)

<SCRIPT LANGUAGE='JavaScript'>

/**//*

* Added by LiuXiaoChong 2005.4.25

* 限制输入的日期控件

* Param: txtName 为要限制输入的文本框的名称

*

* 功能描述:1,只能输入数字

* 2,左右键可以移动编辑焦点

* 3,上下键可以对数据进行微调

* 4,控件包含了对日期的合法校验

*/

function regDateControl(txtName)

{

var oInput = document.getElementById(txtName);

oInput.middleChar = '-';

oInput.selectIndex = 1; //默认选中年

oInput.maxLength = 10;

oInput.style.imeMode = 'disabled';

oInput.value = specialText_GetDate(oInput.middleChar);

oInput.charWidth = oInput.createTextRange().boundingWidth / oInput.maxLength;

//注册单击事件

oInput.onclick = specialText_ClickEvent;

oInput.onkeydown = specialText_KeyDownEvent;

oInput.onfocus = function(){specialText_SelectYear(this);}

oInput.onblur = function()

{

specialText_validYear(this);

specialText_validMonth(this);

specialText_validDate(this);

}

//屏蔽鼠标右键和拖动操作

oInput.oncontextmenu = function(){return false;}

oInput.ondrop = function(){return false;}

}

//鼠标单击,根据位置对日期进行分体选择

function specialText_ClickEvent()

{

event.cancelBubble = true;

specialText_validYear(this);

specialText_validMonth(this);

specialText_validDate(this);

if(event.offsetX <= specialText_getCharWidth(this.charWidth,4))

specialText_SelectYear(this);

else if(event.offsetX > specialText_getCharWidth(this.charWidth,4)

&& event.offsetX <= specialText_getCharWidth(this.charWidth,7))

specialText_SelectMonth(this);

else if(event.offsetX > specialText_getCharWidth(this.charWidth,7))

specialText_SelectDate(this);

}

//选中年份

function specialText_SelectYear(oInput)

{

var oRange = oInput.createTextRange();

oRange.moveStart('character',0);

oRange.moveEnd('character',-6);

//代表选中了年

oInput.selectIndex = 1;

oRange.select();

}

//选中月份

function specialText_SelectMonth(oInput)

{

var oRange = oInput.createTextRange();

oRange.moveStart('character',5);

oRange.moveEnd('character',-3);

//代表选中了月

oInput.selectIndex = 2;

oRange.select();

}

//选中日期

function specialText_SelectDate(oInput)

{

var oRange = oInput.createTextRange();

oRange.moveStart('character',8);

//代表选中了日期

oInput.selectIndex = 3;

oRange.select();

}

//校验年份有效性

function specialText_validYear(oInput)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strYear = arrValue[0];

if(parseInt(strYear,10) == 0)

arrValue[0] = 2000;

//如果年份小于4位,则在2000基础上增加

else if(strYear.length < 4)

arrValue[0] = 2000 + parseInt(strYear,10);

oInput.value = arrValue.join(oInput.middleChar);

}

//校验月份有效性

function specialText_validMonth(oInput)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strMonth = arrValue[1];

//如果月份输入了0,则按1月处理

if(parseInt(strMonth,10) == 0)

arrValue[1] = '01';

//如果月份是一位,则前面补0

else if(strMonth.length < 2)

arrValue[1] = '0' + strMonth;

//如果月份大于12月,自动转为12月

else if(parseInt(strMonth,10) > 12)

arrValue[1] = '12';

oInput.value = arrValue.join(oInput.middleChar);

}

//校验日期有效性

function specialText_validDate(oInput)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strYear = arrValue[0];

var strMonth = arrValue[1];

var strDate = arrValue[2];

var intMonth = parseInt(strMonth,10);

if(parseInt(strDate,10) == 0)

arrValue[2] = '01';

else if(strDate.length < 2)

arrValue[2] = '0' + strDate;

else

{

//如果超过了月份的最大天数,则置为最大天数

var monthMaxDates = specialText_getMonthDates(strYear,strMonth);

if(parseInt(strDate,10) > monthMaxDates)

arrValue[2] = monthMaxDates;

}

oInput.value = arrValue.join(oInput.middleChar);

}

function specialText_YearAdd(oInput,isMinus)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strYear = arrValue[0];

if(isMinus)

{

arrValue[0] = parseInt(strYear,10) - 1;

if(parseInt(arrValue[0],10) < 1)

arrValue[0] = '0001';

}

else

arrValue[0] = parseInt(strYear,10) + 1;

oInput.value = arrValue.join(oInput.middleChar);

specialText_validYear(oInput);

specialText_SelectYear(oInput);

}

function specialText_MonthAdd(oInput,isMinus)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strMonth = arrValue[1];

if(isMinus)

{

arrValue[1] = parseInt(strMonth,10) - 1;

if(parseInt(arrValue[1],10) == 0)

arrValue[1] = '12';

}

else

{

arrValue[1] = parseInt(strMonth,10) + 1;

if(parseInt(arrValue[1],10) == 13)

arrValue[1] = '01';

}

oInput.value = arrValue.join(oInput.middleChar);

specialText_validMonth(oInput);

specialText_SelectMonth(oInput);

}

function specialText_DateAdd(oInput,isMinus)

{

var arrValue = oInput.value.split(oInput.middleChar);

var strYear = arrValue[0];

var strMonth = arrValue[1];

var strDate = arrValue[2];

var monthMaxDates = specialText_getMonthDates(strYear,strMonth);

if(isMinus)

{

arrValue[2] = parseInt(strDate,10) - 1;

if(parseInt(arrValue[2],10) == 0)

arrValue[2] = monthMaxDates;

}

else

{

arrValue[2] = parseInt(strDate,10) + 1;

if(parseInt(arrValue[2],10) == (monthMaxDates + 1))

arrValue[2] = '01';

}

oInput.value = arrValue.join(oInput.middleChar);

specialText_validDate(oInput);

specialText_SelectDate(oInput);

}

function specialText_KeyDownEvent()

{

//如果按了数字键

if((event.keyCode >= 48 && event.keyCode <= 57) ||

(event.keyCode >= 96 && event.keyCode <= 105))

{

var oRange = document.selection.createRange();

if(oRange.text.indexOf(this.middleChar) != -1)

event.returnValue = false;

else

event.returnValue = true;

}

//如果按了方向键

else if(event.keyCode >= 37 && event.keyCode <= 40)

{

event.returnValue = false;

var keyCode = event.keyCode;

//按了左键

if(keyCode == 37)

{

if(this.selectIndex == 1)

{

specialText_validYear(this);

specialText_SelectDate(this);

}

else if(this.selectIndex == 2)

{

specialText_validMonth(this);

specialText_SelectYear(this);

}

else if(this.selectIndex == 3)

{

specialText_validDate(this);

specialText_SelectMonth(this);

}

}

//按了右键

if(keyCode == 39)

{

if(this.selectIndex == 1)

{

specialText_validYear(this);

specialText_SelectMonth(this);

}

else if(this.selectIndex == 2)

{

specialText_validMonth(this);

specialText_SelectDate(this);

}

else if(this.selectIndex == 3)

{

specialText_validDate(this);

specialText_SelectYear(this);

}

}

//按了上键

if(keyCode == 38)

{

if(this.selectIndex == 1)

{

specialText_validYear(this);

specialText_YearAdd(this,true);

}

else if(this.selectIndex == 2)

{

specialText_validMonth(this);

specialText_MonthAdd(this,true);

}

else if(this.selectIndex == 3)

{

specialText_validDate(this);

specialText_DateAdd(this,true);

}

}

//按了下键

if(keyCode == 40)

{

if(this.selectIndex == 1)

{

specialText_validYear(this);

specialText_YearAdd(this,false);

}

else if(this.selectIndex == 2)

{

specialText_validMonth(this);

specialText_MonthAdd(this,false);

}

else if(this.selectIndex == 3)

{

specialText_validDate(this);

specialText_DateAdd(this,false);

}

}

}

//如果按了F5 或 TAB,不屏蔽

else if(event.keyCode == 116 || event.keyCode == 9)

event.returnValue = true;

else

{

event.returnValue = false;

event.keyCode = 0;

}

}

/**//*---------------------辅助函数-----------------------*/

//得到默认日期

function specialText_GetDate(middleChar)

{

var oDate = new Date();

return oDate.getYear() + middleChar

+ (oDate.getMonth() < 10 ? ('0' + oDate.getMonth()) : oDate.getMonth()) + middleChar

+ (oDate.getDate() < 10 ? ('0' + oDate.getDate()) : oDate.getDate());

}

//得到字符像素宽度

function specialText_getCharWidth(charWidth,charNum)

{

return charNum * charWidth;

}

//得到某年某月的最大天数

function specialText_getMonthDates(strYear,strMonth)

{

var intMonth = parseInt(strMonth,10);

if(intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7

|| intMonth == 8 || intMonth == 10 || intMonth == 12)

return 31;

//处理30天的月份

else if(intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11)

return 30;

//处理2月份

else

{

//闰年

if(specialText_isLeapYear(strYear))

return 29;

//平年

else

return 28;

}

}

//判断是否是闰年

function specialText_isLeapYear(strYear)

{

var intYear = parseInt(strYear,10);

if((intYear % 4 == 0 && intYear % 100 != 0) ||

(intYear % 100 == 0 && intYear % 400 == 0))

return true;

else

return false;

}

/**//*----------------------------可以放到外部JS中 DateInputControl.js--------------------*/

function init()

{

regDateControl('date1');

}

</SCRIPT>

<body onload='init()'>

<INPUT TYPE='text' NAME='date1'>

</body>

Javascript 相关文章推荐
用一段js程序来实现动画功能
Mar 06 Javascript
checkbox 多选框 联动实现代码
Oct 22 Javascript
Script的加载方法小结
Jan 12 Javascript
javascript自定义的addClass()方法
May 28 Javascript
JavaScript删除指定子元素代码实例
Jan 13 Javascript
js使用DOM设置单选按钮、复选框及下拉菜单的方法
Jan 20 Javascript
使用impress.js制作幻灯片
Sep 09 Javascript
jQuery使用contains过滤器实现精确匹配方法详解
Feb 25 Javascript
jQuery简单实现点击文本框复制内容到剪贴板上的方法
Aug 01 Javascript
BootStrap栅格系统、表单样式与按钮样式源码解析
Jan 20 Javascript
关于jQuery中fade(),show()起始位置的一点小发现
Apr 25 jQuery
vue实现虚拟列表功能的代码
Jul 28 Javascript
jquery ajax 检测用户注册时用户名是否存在
Nov 03 #Javascript
基于JQuery框架的AJAX实例代码
Nov 03 #Javascript
Jquery AJAX 框架的使用方法
Nov 03 #Javascript
JQuery打造PHP的AJAX表单提交实例
Nov 03 #Javascript
jQuery get和post 方法传值注意事项
Nov 03 #Javascript
js window.onload 加载多个函数的方法
Nov 02 #Javascript
深入认识javascript中的eval函数
Nov 02 #Javascript
You might like
php中计算未知长度的字符串哪个字符出现的次数最多的代码
2012/08/14 PHP
深入学习微信网址链接解封的防封原理visit_type
2019/08/15 PHP
javascript 禁止复制网页
2009/06/11 Javascript
Jquery替换已存在于element上的event的方法
2010/03/09 Javascript
网络图片延迟加载实现代码 超越jquery控件
2010/03/27 Javascript
JavaScript 大数据相加的问题
2011/08/03 Javascript
自己做的模拟模态对话框实现代码
2012/05/23 Javascript
javascript跑马灯悬停放大效果实现代码
2012/12/12 Javascript
js遍历json的key和value的实例
2017/01/22 Javascript
js使用原型对象(prototype)需要注意的地方
2017/08/28 Javascript
Vue结合SignalR实现前后端实时消息同步
2017/09/19 Javascript
Vuex 使用 v-model 配合 state的方法
2018/11/13 Javascript
简单分析js中的this的原理
2019/08/31 Javascript
JS+DIV实现拖动效果
2020/02/11 Javascript
ElementUI中el-tree节点的操作的实现
2020/02/27 Javascript
Vue实现计算器计算效果
2020/08/17 Javascript
JS异步宏队列微队列原理详解
2020/09/09 Javascript
[52:29]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#3Secret VS OG第三局
2016/03/03 DOTA
[56:12]LGD vs Optic Supermajor小组赛D组胜者组决赛 BO3 第一场 6.3
2018/06/04 DOTA
删除目录下相同文件的python代码(逐级优化)
2012/05/25 Python
浅谈flask截获所有访问及before/after_request修饰器
2018/01/18 Python
Python实现的寻找前5个默尼森数算法示例
2018/03/25 Python
python自定义函数实现一个数的三次方计算方法
2019/01/20 Python
python卸载后再次安装遇到的问题解决
2019/07/10 Python
python3 webp转gif格式的实现示例
2019/12/10 Python
python 操作excel表格的方法
2020/12/05 Python
探索HTML5本地存储功能运用技巧
2016/03/02 HTML / CSS
大学生毕业自我鉴定
2013/11/06 职场文书
个人贷款承诺书
2014/03/28 职场文书
十八大标语口号
2014/10/09 职场文书
大学感恩节活动策划方案
2014/10/11 职场文书
简单谈谈Python面向对象的相关知识
2021/06/28 Python
python中使用 unittest.TestCase单元测试的用例详解
2021/08/30 Python
JavaScript 事件捕获冒泡与捕获详情
2021/11/11 Javascript
十大经典日本动漫排行榜 海贼王第三,犬夜叉仅第八
2022/03/18 日漫
vue实现列表拖拽排序的示例代码
2022/04/08 Vue.js