javascript限制输入脚本大全
JavaScript输入 脚本 限制 大全
2023-06-13 09:14:13 时间
1.只能输入汉字的
<inputonkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\u4E00-\u9FA5]/g,""))">
2.只能输入数字的:
<inputonkeyup="value=value.replace(/[^\d]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))">
简易禁止输入汉字
<inputtype="text"style="ime-mode:disabled">
输入数字和小数点:
onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,"")"
javascript只能输入数字和":".2007-11-2415:50<inputtype=textid="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,"")"/>
只能数字和":",例如在输入时间的时候可以用到。
<inputtype=textid="aa"onkeyup="value=value.replace(/[^\w&=]|_/ig,"")"onblur="value=value.replace(/[^\w&=]|_/ig,"")"/>
只能输入字母和等号,不能输入汉字。
其它的东西:
只能输入数字的脚本javascript--
1.<inputonkeyup="this.value=this.value.replace(/\D/g,"")"
onafterpaste="this.value=this.value.replace(/\D/g,"")">
上半句意思是键盘键入只能是数字,下半句是粘贴也只能是数字
2.<inputname=txt1onchange="if(/\D/.test(this.value)){alert("只能输入数字");this.value="";}">
3.<inputonkeyup="if(isNaN(value))execCommand("undo")"onafterpaste="if(isNaN(value))execCommand("undo")">
JavaScript限制只能输入数字和英文--
functionisregname(checkobj)
{
varcheckOK="0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
varcheckStr=checkobj;
varallValid=true;
vardecPoints=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只能输入英文和数字的输入框
<inputonkeyup="value=value.replace(/[\W]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))">
5.可以用Javascript对文本框进行检查,过滤掉非0-9的字符。
<scriptlanguage="javascript"event="onkeydown"for="document">
if(event.srcElement.name=="TextBox1")
{
if(!KeyIsNumber(event.keyCode))
{
returnfalse;//这句话最关键
}
}
</script>
<scriptlanguage="javascript">
functionKeyIsNumber(KeyCode)
{
//如果输入的字符是在0-9之间,或者是backspace、DEL键
if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46))
{
returntrue;
}
else
{
returnfalse;
}
}
</script>[url=http://blog.csdn.net/xujh/admin/EditPosts.aspx][/url]
6.限制在文本框里面只能输入IP地址格式
<HTML>
<head>
<metahttp-equiv="Content-Type"content="text/HTML;charset=gb2312">
<metaname="GENERATOR"content="MicrosoftFrontPage4.0">
<metaname="ProgId"content="FrontPage.Editor.Document">
<style>
.a3{width:30;border:0;text-align:center}
</style>
<script>
functionmask(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()
returnfalse;
}
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()")
}
}
functionmask_c(obj)
{
clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))
}
</script>
<title>IP地址输入</title>
</head>
<body>IP地址输入
<divstyle="border-width:1;border-color:balck;border-style:solid;width:165;font-size:9pt">
<inputtype=textname=ip1maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip2maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip3maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip4maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>
</div>
</body>
</HTML>
7.限制输入格式的日期控件
-----日期控件的另一种思路,限制输入格式的日期控件-----关于日期输入,现在网上的日期控件很多,在这里,我试着模仿了部分CS程序中的日期输入控件,从另一个角度来考虑日期格式的输入,即输入日期时固定格式,用户只能在程序规定的地方输入数据。
例程如下,希望对大家有所帮助~~~(时间仓促,例程是IEONLY的)
<SCRIPTLANGUAGE="JavaScript">
/**//*
*AddedbyLiuXiaoChong2005.4.25
*限制输入的日期控件
*Param:txtName为要限制输入的文本框的名称
*
*功能描述:1,只能输入数字
*2,左右键可以移动编辑焦点
*3,上下键可以对数据进行微调
*4,控件包含了对日期的合法校验
*/
functionregDateControl(txtName)
{
varoInput=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(){returnfalse;}
oInput.ondrop=function(){returnfalse;}
}
//鼠标单击,根据位置对日期进行分体选择
functionspecialText_ClickEvent()
{
event.cancelBubble=true;
specialText_validYear(this);
specialText_validMonth(this);
specialText_validDate(this);
if(event.offsetX<=specialText_getCharWidth(this.charWidth,4))
specialText_SelectYear(this);
elseif(event.offsetX>specialText_getCharWidth(this.charWidth,4)
&&event.offsetX<=specialText_getCharWidth(this.charWidth,7))
specialText_SelectMonth(this);
elseif(event.offsetX>specialText_getCharWidth(this.charWidth,7))
specialText_SelectDate(this);
}
//选中年份
functionspecialText_SelectYear(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",0);
oRange.moveEnd("character",-6);
//代表选中了年
oInput.selectIndex=1;
oRange.select();
}
//选中月份
functionspecialText_SelectMonth(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",5);
oRange.moveEnd("character",-3);
//代表选中了月
oInput.selectIndex=2;
oRange.select();
}
//选中日期
functionspecialText_SelectDate(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",8);
//代表选中了日期
oInput.selectIndex=3;
oRange.select();
}
//校验年份有效性
functionspecialText_validYear(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
if(parseInt(strYear,10)==0)
arrValue[0]=2000;
//如果年份小于4位,则在2000基础上增加
elseif(strYear.length<4)
arrValue[0]=2000+parseInt(strYear,10);
oInput.value=arrValue.join(oInput.middleChar);
}
//校验月份有效性
functionspecialText_validMonth(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrMonth=arrValue[1];
//如果月份输入了0,则按1月处理
if(parseInt(strMonth,10)==0)
arrValue[1]="01";
//如果月份是一位,则前面补0
elseif(strMonth.length<2)
arrValue[1]="0"+strMonth;
//如果月份大于12月,自动转为12月
elseif(parseInt(strMonth,10)>12)
arrValue[1]="12";
oInput.value=arrValue.join(oInput.middleChar);
}
//校验日期有效性
functionspecialText_validDate(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
varstrMonth=arrValue[1];
varstrDate=arrValue[2];
varintMonth=parseInt(strMonth,10);
if(parseInt(strDate,10)==0)
arrValue[2]="01";
elseif(strDate.length<2)
arrValue[2]="0"+strDate;
else
{
//如果超过了月份的最大天数,则置为最大天数
varmonthMaxDates=specialText_getMonthDates(strYear,strMonth);
if(parseInt(strDate,10)>monthMaxDates)
arrValue[2]=monthMaxDates;
}
oInput.value=arrValue.join(oInput.middleChar);
}
functionspecialText_YearAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=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);
}
functionspecialText_MonthAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrMonth=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);
}
functionspecialText_DateAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
varstrMonth=arrValue[1];
varstrDate=arrValue[2];
varmonthMaxDates=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);
}
functionspecialText_KeyDownEvent()
{
//如果按了数字键
if((event.keyCode>=48&&event.keyCode<=57)||
(event.keyCode>=96&&event.keyCode<=105))
{
varoRange=document.selection.createRange();
if(oRange.text.indexOf(this.middleChar)!=-1)
event.returnValue=false;
else
event.returnValue=true;
}
//如果按了方向键
elseif(event.keyCode>=37&&event.keyCode<=40)
{
event.returnValue=false;
varkeyCode=event.keyCode;
//按了左键
if(keyCode==37)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_SelectDate(this);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_SelectYear(this);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_SelectMonth(this);
}
}
//按了右键
if(keyCode==39)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_SelectMonth(this);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_SelectDate(this);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_SelectYear(this);
}
}
//按了上键
if(keyCode==38)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_YearAdd(this,true);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,true);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_DateAdd(this,true);
}
}
//按了下键
if(keyCode==40)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_YearAdd(this,false);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,false);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_DateAdd(this,false);
}
}
}
//如果按了F5或TAB,不屏蔽
elseif(event.keyCode==116||event.keyCode==9)
event.returnValue=true;
else
{
event.returnValue=false;
event.keyCode=0;
}
}
/**//*---------------------辅助函数-----------------------*/
//得到默认日期
functionspecialText_GetDate(middleChar)
{
varoDate=newDate();
returnoDate.getYear()+middleChar
+(oDate.getMonth()<10?("0"+oDate.getMonth()):oDate.getMonth())+middleChar
+(oDate.getDate()<10?("0"+oDate.getDate()):oDate.getDate());
}
//得到字符像素宽度
functionspecialText_getCharWidth(charWidth,charNum)
{
returncharNum*charWidth;
}
//得到某年某月的最大天数
functionspecialText_getMonthDates(strYear,strMonth)
{
varintMonth=parseInt(strMonth,10);
if(intMonth==1||intMonth==3||intMonth==5||intMonth==7
||intMonth==8||intMonth==10||intMonth==12)
return31;
//处理30天的月份
elseif(intMonth==4||intMonth==6||intMonth==9||intMonth==11)
return30;
//处理2月份
else
{
//闰年
if(specialText_isLeapYear(strYear))
return29;
//平年
else
return28;
}
}
//判断是否是闰年
functionspecialText_isLeapYear(strYear)
{
varintYear=parseInt(strYear,10);
if((intYear%4==0&&intYear%100!=0)||
(intYear%100==0&&intYear%400==0))
returntrue;
else
returnfalse;
}
/**//*----------------------------可以放到外部JS中DateInputControl.js--------------------*/
functioninit()
{
regDateControl("date1");
}
</SCRIPT>
<bodyonload="init()">
<INPUTTYPE="text"NAME="date1">
</body>
<inputonkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\u4E00-\u9FA5]/g,""))">
2.只能输入数字的:
<inputonkeyup="value=value.replace(/[^\d]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))">
简易禁止输入汉字
<inputtype="text"style="ime-mode:disabled">
输入数字和小数点:
onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,"")"
javascript只能输入数字和":".2007-11-2415:50<inputtype=textid="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,"")"/>
只能数字和":",例如在输入时间的时候可以用到。
<inputtype=textid="aa"onkeyup="value=value.replace(/[^\w&=]|_/ig,"")"onblur="value=value.replace(/[^\w&=]|_/ig,"")"/>
只能输入字母和等号,不能输入汉字。
其它的东西:
只能输入数字的脚本javascript--
1.<inputonkeyup="this.value=this.value.replace(/\D/g,"")"
onafterpaste="this.value=this.value.replace(/\D/g,"")">
上半句意思是键盘键入只能是数字,下半句是粘贴也只能是数字
2.<inputname=txt1onchange="if(/\D/.test(this.value)){alert("只能输入数字");this.value="";}">
3.<inputonkeyup="if(isNaN(value))execCommand("undo")"onafterpaste="if(isNaN(value))execCommand("undo")">
JavaScript限制只能输入数字和英文--
functionisregname(checkobj)
{
varcheckOK="0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
varcheckStr=checkobj;
varallValid=true;
vardecPoints=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只能输入英文和数字的输入框
<inputonkeyup="value=value.replace(/[\W]/g,"")"onbeforepaste="clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))">
5.可以用Javascript对文本框进行检查,过滤掉非0-9的字符。
<scriptlanguage="javascript"event="onkeydown"for="document">
if(event.srcElement.name=="TextBox1")
{
if(!KeyIsNumber(event.keyCode))
{
returnfalse;//这句话最关键
}
}
</script>
<scriptlanguage="javascript">
functionKeyIsNumber(KeyCode)
{
//如果输入的字符是在0-9之间,或者是backspace、DEL键
if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46))
{
returntrue;
}
else
{
returnfalse;
}
}
</script>[url=http://blog.csdn.net/xujh/admin/EditPosts.aspx][/url]
6.限制在文本框里面只能输入IP地址格式
<HTML>
<head>
<metahttp-equiv="Content-Type"content="text/HTML;charset=gb2312">
<metaname="GENERATOR"content="MicrosoftFrontPage4.0">
<metaname="ProgId"content="FrontPage.Editor.Document">
<style>
.a3{width:30;border:0;text-align:center}
</style>
<script>
functionmask(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()
returnfalse;
}
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()")
}
}
functionmask_c(obj)
{
clipboardData.setData("text",clipboardData.getData("text").replace(/[^\d]/g,""))
}
</script>
<title>IP地址输入</title>
</head>
<body>IP地址输入
<divstyle="border-width:1;border-color:balck;border-style:solid;width:165;font-size:9pt">
<inputtype=textname=ip1maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip2maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip3maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>.
<inputtype=textname=ip4maxlength=3class=a3onkeyup="mask(this)"onbeforepaste=mask_c()>
</div>
</body>
</HTML>
7.限制输入格式的日期控件
-----日期控件的另一种思路,限制输入格式的日期控件-----关于日期输入,现在网上的日期控件很多,在这里,我试着模仿了部分CS程序中的日期输入控件,从另一个角度来考虑日期格式的输入,即输入日期时固定格式,用户只能在程序规定的地方输入数据。
例程如下,希望对大家有所帮助~~~(时间仓促,例程是IEONLY的)
<SCRIPTLANGUAGE="JavaScript">
/**//*
*AddedbyLiuXiaoChong2005.4.25
*限制输入的日期控件
*Param:txtName为要限制输入的文本框的名称
*
*功能描述:1,只能输入数字
*2,左右键可以移动编辑焦点
*3,上下键可以对数据进行微调
*4,控件包含了对日期的合法校验
*/
functionregDateControl(txtName)
{
varoInput=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(){returnfalse;}
oInput.ondrop=function(){returnfalse;}
}
//鼠标单击,根据位置对日期进行分体选择
functionspecialText_ClickEvent()
{
event.cancelBubble=true;
specialText_validYear(this);
specialText_validMonth(this);
specialText_validDate(this);
if(event.offsetX<=specialText_getCharWidth(this.charWidth,4))
specialText_SelectYear(this);
elseif(event.offsetX>specialText_getCharWidth(this.charWidth,4)
&&event.offsetX<=specialText_getCharWidth(this.charWidth,7))
specialText_SelectMonth(this);
elseif(event.offsetX>specialText_getCharWidth(this.charWidth,7))
specialText_SelectDate(this);
}
//选中年份
functionspecialText_SelectYear(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",0);
oRange.moveEnd("character",-6);
//代表选中了年
oInput.selectIndex=1;
oRange.select();
}
//选中月份
functionspecialText_SelectMonth(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",5);
oRange.moveEnd("character",-3);
//代表选中了月
oInput.selectIndex=2;
oRange.select();
}
//选中日期
functionspecialText_SelectDate(oInput)
{
varoRange=oInput.createTextRange();
oRange.moveStart("character",8);
//代表选中了日期
oInput.selectIndex=3;
oRange.select();
}
//校验年份有效性
functionspecialText_validYear(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
if(parseInt(strYear,10)==0)
arrValue[0]=2000;
//如果年份小于4位,则在2000基础上增加
elseif(strYear.length<4)
arrValue[0]=2000+parseInt(strYear,10);
oInput.value=arrValue.join(oInput.middleChar);
}
//校验月份有效性
functionspecialText_validMonth(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrMonth=arrValue[1];
//如果月份输入了0,则按1月处理
if(parseInt(strMonth,10)==0)
arrValue[1]="01";
//如果月份是一位,则前面补0
elseif(strMonth.length<2)
arrValue[1]="0"+strMonth;
//如果月份大于12月,自动转为12月
elseif(parseInt(strMonth,10)>12)
arrValue[1]="12";
oInput.value=arrValue.join(oInput.middleChar);
}
//校验日期有效性
functionspecialText_validDate(oInput)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
varstrMonth=arrValue[1];
varstrDate=arrValue[2];
varintMonth=parseInt(strMonth,10);
if(parseInt(strDate,10)==0)
arrValue[2]="01";
elseif(strDate.length<2)
arrValue[2]="0"+strDate;
else
{
//如果超过了月份的最大天数,则置为最大天数
varmonthMaxDates=specialText_getMonthDates(strYear,strMonth);
if(parseInt(strDate,10)>monthMaxDates)
arrValue[2]=monthMaxDates;
}
oInput.value=arrValue.join(oInput.middleChar);
}
functionspecialText_YearAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=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);
}
functionspecialText_MonthAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrMonth=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);
}
functionspecialText_DateAdd(oInput,isMinus)
{
vararrValue=oInput.value.split(oInput.middleChar);
varstrYear=arrValue[0];
varstrMonth=arrValue[1];
varstrDate=arrValue[2];
varmonthMaxDates=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);
}
functionspecialText_KeyDownEvent()
{
//如果按了数字键
if((event.keyCode>=48&&event.keyCode<=57)||
(event.keyCode>=96&&event.keyCode<=105))
{
varoRange=document.selection.createRange();
if(oRange.text.indexOf(this.middleChar)!=-1)
event.returnValue=false;
else
event.returnValue=true;
}
//如果按了方向键
elseif(event.keyCode>=37&&event.keyCode<=40)
{
event.returnValue=false;
varkeyCode=event.keyCode;
//按了左键
if(keyCode==37)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_SelectDate(this);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_SelectYear(this);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_SelectMonth(this);
}
}
//按了右键
if(keyCode==39)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_SelectMonth(this);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_SelectDate(this);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_SelectYear(this);
}
}
//按了上键
if(keyCode==38)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_YearAdd(this,true);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,true);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_DateAdd(this,true);
}
}
//按了下键
if(keyCode==40)
{
if(this.selectIndex==1)
{
specialText_validYear(this);
specialText_YearAdd(this,false);
}
elseif(this.selectIndex==2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,false);
}
elseif(this.selectIndex==3)
{
specialText_validDate(this);
specialText_DateAdd(this,false);
}
}
}
//如果按了F5或TAB,不屏蔽
elseif(event.keyCode==116||event.keyCode==9)
event.returnValue=true;
else
{
event.returnValue=false;
event.keyCode=0;
}
}
/**//*---------------------辅助函数-----------------------*/
//得到默认日期
functionspecialText_GetDate(middleChar)
{
varoDate=newDate();
returnoDate.getYear()+middleChar
+(oDate.getMonth()<10?("0"+oDate.getMonth()):oDate.getMonth())+middleChar
+(oDate.getDate()<10?("0"+oDate.getDate()):oDate.getDate());
}
//得到字符像素宽度
functionspecialText_getCharWidth(charWidth,charNum)
{
returncharNum*charWidth;
}
//得到某年某月的最大天数
functionspecialText_getMonthDates(strYear,strMonth)
{
varintMonth=parseInt(strMonth,10);
if(intMonth==1||intMonth==3||intMonth==5||intMonth==7
||intMonth==8||intMonth==10||intMonth==12)
return31;
//处理30天的月份
elseif(intMonth==4||intMonth==6||intMonth==9||intMonth==11)
return30;
//处理2月份
else
{
//闰年
if(specialText_isLeapYear(strYear))
return29;
//平年
else
return28;
}
}
//判断是否是闰年
functionspecialText_isLeapYear(strYear)
{
varintYear=parseInt(strYear,10);
if((intYear%4==0&&intYear%100!=0)||
(intYear%100==0&&intYear%400==0))
returntrue;
else
returnfalse;
}
/**//*----------------------------可以放到外部JS中DateInputControl.js--------------------*/
functioninit()
{
regDateControl("date1");
}
</SCRIPT>
<bodyonload="init()">
<INPUTTYPE="text"NAME="date1">
</body>
相关文章
- Ajax的面试题_javascript面试题及答案
- JavaScript案例:按键输入内容,模拟自动大字号
- 使用JavaScript的padStart()和padEnd()格式化字符串的技巧
- JavaScript判断输入的参数是否是个合格的URL详解编程语言
- JavaScript判断输入的参数是否是国内合法的邮编地址详解编程语言
- JavaScript控制输入框只能输入非负正整数详解编程语言
- JavaScript学习总结(二十)——Javascript非构造函数的继承详解编程语言
- JavaScript学习总结(九)——Javascript面向(基于)对象编程详解编程语言
- javascript jssdk退出微信的方法详解编程语言
- 最全 JavaScript Array 方法 详解
- javascript字典探测用户名工具
- javascript中的对象和数组的应用技巧
- javascript表单日期选择效果
- javascript写的一个表单动态输入提示的代码
- javascript之文本框输入四个数字自动加空格的脚本
- Javascript更新JavaScript数组的uniq方法
- javascript新手语法小结
- Javascript浅拷贝、深拷贝的实现代码
- Javascript阻止javascript事件冒泡,获取控件ID值
- 用Javascript评估用户输入密码的强度(Knockout版)
- javascript中比较字符串是否相等的方法
- 5分钟理解JavaScript中this用法分享
- 利用noesis.Javascript开源组件.Net中执行javascript脚本
- Javascript写入txt和读取txt文件示例
- JavaScript中统计Textarea字数并提示还能输入的字符
- javascript实现依次输入input自动定焦
- Javascript正则控制文本框只能输入整数或浮点数