Click here to Skip to main content
15,896,456 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
JavaScript
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;
                }
            }
        }


HTML
<INPUT id="txtChar" >
Posted
Updated 17-Nov-15 19:44pm
v2
Comments
Maciej Los 18-Nov-15 1:44am    
What's the question?
Suvendu Shekhar Giri 18-Nov-15 1:46am    
..and what is the problem?
Patrice T 18-Nov-15 3:18am    
And you have a question ? a problem ?

1 solution

XML
<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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900