javascript 限制輸入腳本大全
更新時(shí)間:2009年11月03日 23:58:36 作者:
javascript限制輸入腳本大全 ,方便網(wǎng)頁(yè)設(shè)計(jì)的朋友經(jīng)常能用的到,建議收藏下。代碼有點(diǎn)慢,大家慢慢看啊。
1.只能輸入漢字的
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">
2.只能輸入數(shù)字的:
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
簡(jiǎn)易禁止輸入漢字
<input type="text" style="ime-mode:disabled">
輸入數(shù)字和小數(shù)點(diǎn):
onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
javascript 只能輸入數(shù)字和":".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,'')"/>
只能數(shù)字和":",例如在輸入時(shí)間的時(shí)候可以用到。
<input type=text id="aa" onkeyup="value=value.replace(/[^\w&=]|_/ig,'')" onblur="value=value.replace(/[^\w&=]|_/ig,'')" />
只能輸入字母和等號(hào),不能輸入漢字。
其它的東西:
只能輸入數(shù)字的腳本javascript- -
1.<input onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')">
上半句意思是鍵盤鍵入只能是數(shù)字,下半句是粘貼也只能是數(shù)字
2.<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數(shù)字');this.value='';}">
3.<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
JavaScript限制只能輸入數(shù)字和英文- -
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("[會(huì)員代號(hào)]不符合規(guī)范,會(huì)員代號(hào)只能是英文字母或數(shù)字");
obj.loginname.focus();
return(false);
}
if(!(isregname(obj.password.value))){
alert("[密碼]不符合規(guī)范,密碼只能是英文字母或數(shù)字");
obj.password.focus();
return(false);
}
4.javascript只能輸入英文和數(shù)字的輸入框
<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
5.可以用Javascript對(duì)文本框進(jìn)行檢查,過濾掉非0-9的字符。
<script language="javascript" event="onkeydown" for="document">
if(event.srcElement.name=='TextBox1')
{
if(!KeyIsNumber(event.keyCode))
{
return false;//這句話最關(guān)鍵
}
}
</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地址錯(cuò)誤!")
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.限制輸入格式的日期控件
----- 日期控件的另一種思路,限制輸入格式的日期控件 ----- 關(guān)于日期輸入,現(xiàn)在網(wǎng)上的日期控件很多,在這里,我試著模仿了部分CS程序中的日期輸入控件,從另一個(gè)角度來(lái)考慮日期格式的輸入,即輸入日期時(shí)固定格式,用戶只能在程序規(guī)定的地方輸入數(shù)據(jù)。
例程如下,希望對(duì)大家有所幫助~~~ (時(shí)間倉(cāng)促,例程是IE ONLY的)
<SCRIPT LANGUAGE='JavaScript'>
/**//*
* Added by LiuXiaoChong 2005.4.25
* 限制輸入的日期控件
* Param: txtName 為要限制輸入的文本框的名稱
*
* 功能描述:1,只能輸入數(shù)字
* 2,左右鍵可以移動(dòng)編輯焦點(diǎn)
* 3,上下鍵可以對(duì)數(shù)據(jù)進(jìn)行微調(diào)
* 4,控件包含了對(duì)日期的合法校驗(yàn)
*/
function regDateControl(txtName)
{
var oInput = document.getElementById(txtName);
oInput.middleChar = '-';
oInput.selectIndex = 1; //默認(rèn)選中年
oInput.maxLength = 10;
oInput.style.imeMode = 'disabled';
oInput.value = specialText_GetDate(oInput.middleChar);
oInput.charWidth = oInput.createTextRange().boundingWidth / oInput.maxLength;
//注冊(cè)單擊事件
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);
}
//屏蔽鼠標(biāo)右鍵和拖動(dòng)操作
oInput.oncontextmenu = function(){return false;}
oInput.ondrop = function(){return false;}
}
//鼠標(biāo)單擊,根據(jù)位置對(duì)日期進(jìn)行分體選擇
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();
}
//校驗(yàn)?zāi)攴萦行?
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基礎(chǔ)上增加
else if(strYear.length < 4)
arrValue[0] = 2000 + parseInt(strYear,10);
oInput.value = arrValue.join(oInput.middleChar);
}
//校驗(yàn)月份有效性
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';
//如果月份是一位,則前面補(bǔ)0
else if(strMonth.length < 2)
arrValue[1] = '0' + strMonth;
//如果月份大于12月,自動(dòng)轉(zhuǎn)為12月
else if(parseInt(strMonth,10) > 12)
arrValue[1] = '12';
oInput.value = arrValue.join(oInput.middleChar);
}
//校驗(yàn)日期有效性
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
{
//如果超過了月份的最大天數(shù),則置為最大天數(shù)
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()
{
//如果按了數(shù)字鍵
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;
}
}
/**//*---------------------輔助函數(shù)-----------------------*/
//得到默認(rèn)日期
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;
}
//得到某年某月的最大天數(shù)
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>
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">
2.只能輸入數(shù)字的:
<input onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
簡(jiǎn)易禁止輸入漢字
<input type="text" style="ime-mode:disabled">
輸入數(shù)字和小數(shù)點(diǎn):
onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
javascript 只能輸入數(shù)字和":".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,'')"/>
只能數(shù)字和":",例如在輸入時(shí)間的時(shí)候可以用到。
<input type=text id="aa" onkeyup="value=value.replace(/[^\w&=]|_/ig,'')" onblur="value=value.replace(/[^\w&=]|_/ig,'')" />
只能輸入字母和等號(hào),不能輸入漢字。
其它的東西:
只能輸入數(shù)字的腳本javascript- -
1.<input onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')">
上半句意思是鍵盤鍵入只能是數(shù)字,下半句是粘貼也只能是數(shù)字
2.<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數(shù)字');this.value='';}">
3.<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
JavaScript限制只能輸入數(shù)字和英文- -
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("[會(huì)員代號(hào)]不符合規(guī)范,會(huì)員代號(hào)只能是英文字母或數(shù)字");
obj.loginname.focus();
return(false);
}
if(!(isregname(obj.password.value))){
alert("[密碼]不符合規(guī)范,密碼只能是英文字母或數(shù)字");
obj.password.focus();
return(false);
}
4.javascript只能輸入英文和數(shù)字的輸入框
<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
5.可以用Javascript對(duì)文本框進(jìn)行檢查,過濾掉非0-9的字符。
<script language="javascript" event="onkeydown" for="document">
if(event.srcElement.name=='TextBox1')
{
if(!KeyIsNumber(event.keyCode))
{
return false;//這句話最關(guān)鍵
}
}
</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地址錯(cuò)誤!")
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.限制輸入格式的日期控件
----- 日期控件的另一種思路,限制輸入格式的日期控件 ----- 關(guān)于日期輸入,現(xiàn)在網(wǎng)上的日期控件很多,在這里,我試著模仿了部分CS程序中的日期輸入控件,從另一個(gè)角度來(lái)考慮日期格式的輸入,即輸入日期時(shí)固定格式,用戶只能在程序規(guī)定的地方輸入數(shù)據(jù)。
例程如下,希望對(duì)大家有所幫助~~~ (時(shí)間倉(cāng)促,例程是IE ONLY的)
<SCRIPT LANGUAGE='JavaScript'>
/**//*
* Added by LiuXiaoChong 2005.4.25
* 限制輸入的日期控件
* Param: txtName 為要限制輸入的文本框的名稱
*
* 功能描述:1,只能輸入數(shù)字
* 2,左右鍵可以移動(dòng)編輯焦點(diǎn)
* 3,上下鍵可以對(duì)數(shù)據(jù)進(jìn)行微調(diào)
* 4,控件包含了對(duì)日期的合法校驗(yàn)
*/
function regDateControl(txtName)
{
var oInput = document.getElementById(txtName);
oInput.middleChar = '-';
oInput.selectIndex = 1; //默認(rèn)選中年
oInput.maxLength = 10;
oInput.style.imeMode = 'disabled';
oInput.value = specialText_GetDate(oInput.middleChar);
oInput.charWidth = oInput.createTextRange().boundingWidth / oInput.maxLength;
//注冊(cè)單擊事件
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);
}
//屏蔽鼠標(biāo)右鍵和拖動(dòng)操作
oInput.oncontextmenu = function(){return false;}
oInput.ondrop = function(){return false;}
}
//鼠標(biāo)單擊,根據(jù)位置對(duì)日期進(jìn)行分體選擇
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();
}
//校驗(yàn)?zāi)攴萦行?
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基礎(chǔ)上增加
else if(strYear.length < 4)
arrValue[0] = 2000 + parseInt(strYear,10);
oInput.value = arrValue.join(oInput.middleChar);
}
//校驗(yàn)月份有效性
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';
//如果月份是一位,則前面補(bǔ)0
else if(strMonth.length < 2)
arrValue[1] = '0' + strMonth;
//如果月份大于12月,自動(dòng)轉(zhuǎn)為12月
else if(parseInt(strMonth,10) > 12)
arrValue[1] = '12';
oInput.value = arrValue.join(oInput.middleChar);
}
//校驗(yàn)日期有效性
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
{
//如果超過了月份的最大天數(shù),則置為最大天數(shù)
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()
{
//如果按了數(shù)字鍵
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;
}
}
/**//*---------------------輔助函數(shù)-----------------------*/
//得到默認(rèn)日期
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;
}
//得到某年某月的最大天數(shù)
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>
相關(guān)文章
javascript 循環(huán)調(diào)用示例介紹
循環(huán)調(diào)用,如果已經(jīng)獲取到了結(jié)果,則退出循環(huán),下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以嘗試操作下2013-11-11JS實(shí)現(xiàn)iframe編輯器光標(biāo)位置插入內(nèi)容的方法(兼容IE和Firefox)
這篇文章主要介紹了JS實(shí)現(xiàn)iframe編輯器光標(biāo)位置插入內(nèi)容的方法,可實(shí)現(xiàn)文本與圖片的插入功能,并兼容IE和Firefox瀏覽器,需要的朋友可以參考下2016-06-06比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法
比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法...2007-11-11JavaScript數(shù)據(jù)結(jié)構(gòu)與算法
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法,文章圍繞主題展開數(shù)據(jù)結(jié)構(gòu)與算法的概念,以及幾種常見的數(shù)據(jù)結(jié)構(gòu)是什么,有什么優(yōu)點(diǎn)和缺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07用js實(shí)現(xiàn)table單元格高寬調(diào)整,兼容合并單元格(兼容IE6、7、8、FF)實(shí)例
用js實(shí)現(xiàn)table單元格寬度和高度調(diào)整,有合并單元格也可以的.兼容IE6,7,8以及FF,附上代碼css,html,js三部份,有需要的朋友可以參考一下2013-06-06