HTML
<input type="text" name="year" autofocus onkeypress='validate(event)'><br>
<input type="text" name="month" onkeypress='validate(event)'><br>
JavaScript
<script type="text/javascript">
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}}
</script>
如果我的输入(名称“年”)为空,如何禁用(防止在文本框中写入数字)输入(名称“月”)?当然,如果我要输入年份,如何启用(允许写入数字)它?