日期:2014-05-16  浏览次数:20386 次

javascript特效:只能输入数字
2011-05-23 01:03

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
?<!-- Created by TopStyle Pro Trial Version -? www.bradsoft.com -->
?<title></title>
?
</head>

<body>
<script type="text/javascript">?
// 说明 : 用 Javascript 实现只能输入数字的文本框
// 整理 : CodeBit.cn (? http://www.CodeBit.cn )
?function checkNumber(e) {????
?var key = window.event ? e.keyCode : e.which;???
? var keychar = String.fromCharCode(key);????
? var el = document.getElementById('test');????
? var msg = document.getElementById('msg');????? reg = /\d/;???
?? var result = reg.test(keychar);???
? if(!result)? {?? el.className = "warn";????????? msg.innerHTML = "只能输入数字";????????
? return false; }????
? else? {?????????
? el.className = "";????????? msg.innerHTML = "";???????? return true;???
?? }
?? }?
? </script>?
?<div id="test">

???? 请输入数字:<input type="text" onkeypress="return checkNumber(event);" />
??? <span id="msg"></span>
</div>

</body>
?

?