非常好用,代碼示例如下:
<input onkeypress = "return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
onpaste = "return !clipboardData.getData('text').match(/\D/)"
ondragenter = "return false"
style= "ime-mode:Disabled">
說明
1 隻能輸入0到9和小數點
2 隻能粘貼數字
3 不能拖動内容進來
4 禁止使用輸入法
2. 代碼:
<html><head><meta http-equiv="content-Type" content="text/html;charset=gb2312"><title>js 隻能輸入數字和小數點</title>
<script language="JavaScript" type="text/javascript">
function clearNoNum(obj){ obj.value = obj.value.replace(/[^\d.]/g,""); //清除“數字”和“.”以外的字元
obj.value = obj.value.replace(/^\./g,""); //驗證第一個字元是數字而不是.
obj.value = obj.value.replace(/\.{2,}/g,"."); //隻保留第一個. 清除多餘的.
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
</script>
</head><body>隻能輸入數字和小數點的文本框:<input name="input1" onkeyup="clearNoNum(this)"></body></html>
本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/p/4362499.html,如需轉載請自行聯系原作者