87,904
社区成员
发帖
与我相关
我的任务
分享
<!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" >
<head>
<title>Untitled Page</title>
</head>
<body>
<script>
function regInput(obj, reg, inputStr) {
var docSel = document.selection.createRange()
if (docSel.parentElement().tagName != "INPUT") return false
oSel = docSel.duplicate()
oSel.text = ""
var srcRange = obj.createTextRange()
oSel.setEndPoint("StartToStart", srcRange)
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length);
return reg.test(str)
}
</script>
<input type="text" id="txt" onkeypress = "return regInput(this,/^\d*$/,String.fromCharCode(event.keyCode))">
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
/**
* author: develop_design_level
* date: 2009-10-30
* @param {Object} id
*/
function $(id){
if (document.getElementById) {
return document.getElementById(id);
}
else {
return document.all.id;
}
}
function checkInputText(){
var regExp = /^\d+$/g;
var text = this.value;
if(!regExp.test(text)){
this.value = '0';
}
}
window.onload = function(){
$('testId').onkeyup = checkInputText;
};
</script>
</head>
<body>
<form>
<input type="text" id="testId" value="0">
</form>
</body>
</html>