文本框仅验证年份 [英] text box to validate only years

查看:122
本文介绍了文本框仅验证年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function  isNumberKey(evt,obj)
{
var LIMIT = 5 ;
var charCode =(evt.which)? evt.which:event.keyCode
var txt = obj.value.length;
if ((txt == LIMIT)&&(charCode == 8 ) ){
obj.value = obj.value.toString()。substring( 0 ,txt-1);
}
if (charCode> 31 &&(charCode< ; 48 || charCode> 57 ))
返回 false ;
else {
if (txt< LIMIT){
返回 true ;
}
其他 {
return ;
}
}
}





 <   INPUT     id   =  txtChar   >  

解决方案

 <   html  >  
< head >
< title > 年份验证-JAVASCRIPT < / title >

< script 类型 = text / javascript >
//检查从现在到1900年间传入的数据是否为4位数
function checkIsValidYear(_data)
{
var _thisYear = new Date()。getFullYear();

if(_data.length!= 4){
alert(这是无效的);
返回false;
}

if(!_data.match(/ \d {4} /)){
alert(This is not valid);
返回false;
}

if(parseInt(_data)> _thisYear || parseInt(_data)< 1900){

alert( valid);

return false;

}



return true;

}

< / script >
< / head >
< body >
< 表格 名称 = myForm >
< input type = text name < span class =code-keyword> = theYear maxlength = 4 value = onchange = return checkIsValidYear(this.value); / >
< / body >
< / html > < /跨度>


function isNumberKey(evt, obj) 
         {
            var LIMIT = 5;
            var charCode = (evt.which) ? evt.which : event.keyCode
            var txt = obj.value.length;
            if ((txt == LIMIT) && (charCode == 8)) {
                obj.value = obj.value.toString().substring(0, txt-1);
            }
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            else {
                if (txt < LIMIT) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }



<INPUT id="txtChar" >

解决方案

<html>
<head>
<title>Year Validation-JAVASCRIPT</title>

<script type="text/javascript">
// checks to see if the data passed in is a 4 digit year between now and 1900
function checkIsValidYear(_data)
{
 var _thisYear = new Date().getFullYear();

 if (_data.length != 4){
        alert( "This is not valid");
        return false;
    }

 if (!_data.match(/\d{4}/)){
    alert( "This is not valid");
     return false;
    }

 if (parseInt(_data) > _thisYear || parseInt(_data) < 1900){

    alert( "This is not valid");

    return false;

    }



 return true;

}

</script>
</head>
<body>
<form name="myForm">
<input type="text" name="theYear" maxlength="4" value="" onchange="return checkIsValidYear(this.value) ;"/>
</body>
</html>


这篇关于文本框仅验证年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆