文本框只能输入数值或浮点数

来源:互联网 发布:征途2s网络连接超时 编辑:程序博客网 时间:2024/04/28 05:17
//文本框只能输入数值或浮点数。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)}

html

onkeypress="return regInput(this,/^\d*\.?\d{0,4}$/,String.fromCharCode(event.keyCode))"                        onpaste="return regInput(this,/^\d*\.?\d{0,4}$/,window.clipboardData.getData('Text'))"                        ondrop="return regInput(this,/^\d*\.?\d{0,4}$/,event.dataTransfer.getData('Text'))"




原创粉丝点击