JS控制文本框只能输入整数或带一位小数的浮点数

 

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>无标题页</title>
 <script type="text/javascript" language="javascript">
 //控制文本框只能输入整数或带一位小数的浮点数
 function regInput(obj, inputStr)
 {
  var reg=/^\d*\.?\d{0,1}$/;
  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>

</head>
<body>
    <form id="form1" runat="server">
      
        测试html控件:
        <input onkeypress="return regInput(this,String.fromCharCode(event.keyCode))"
            onpaste="return regInput(this,window.clipboardData.getData('Text'))"
            ondrop="return regInput(this,event.dataTransfer.getData('Text'))">
    </form>
</body>
</html>


 

你可能感兴趣的:(JavaScript,html,server,function,测试,input)