var e = evt || window.event,
key = e.keyCode || e.which
if (
// Backspace, Tab, Enter, Esc, Delete
key == 8 || key == 9 || key == 13 || key == 27 || key == 46 ||
// Ctrl + A
(key == 65 &&event.ctrlKey === true) ||
// Home, End, 四个方向键
key >= 35 &&key <= 40) {
return
}
if (e.shiftKey || e.altKey || e.ctrlKey) {
return false
}
var el = e.target || e.srcElement,
nl = el.getAttribute("data-numbers") || 15,
// 最大小数长度
dl = el.getAttribute("data-decimals") || 2,
val = el.value,
// "." 位置
dotIndex = val.indexOf("."),
rng = caret.call(el),
// 光标在"."左边
rLeft = rng.end <= dotIndex,
// 光标在"."右边
rRight = rng.begin >dotIndex
if (
// 数字
key >= 48 &&key <= 57 ||
// 数字键盘输入的数字
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" />
<title>正则 只能输入数字</title>
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<link rel="stylesheet" href="css/.css" />
</head>
<body>
<input type="text" id="txt" />
<button id="btn">确定</button>
<script>
var txt = document.getElementById('txt')
var btn = document.getElementById('btn')
var reg = /^\d*$/
btn.onclick = function(){
var val = txt.value
if( !reg.test(val)){
alert('只能输入数字!')
}
}
</script>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)