javascript限制输入decimal

<!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>javascript限制输入decimal</title>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	</head>
	<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>
	<body>
	<!-- decimal(7,4)输入限制 -->
    输入decimal(7,4) <input onkeypress = "return regInput(this, /^(\d{1,3}\.\d{0,4})$|^(\d{1,3}\.?)$/, String.fromCharCode(event.keyCode))"
		onpaste = "return regInput(this, /^(\d{1,3}\.\d{0,4})$|^(\d{1,3}\.?)$/, window.clipboardData.getData('Text'))"
		ondrop = "return regInput(this, /^(\d{1,3}\.\d{0,4})$|^(\d{1,3}\.?)$/, event.dataTransfer.getData('Text'))"
		style="ime-mode:Disabled"> 如:123.4567
	</body>
</html>


你可能感兴趣的:(JavaScript,html,XHTML,function,input)