正则:文本框只能输入数字,最多4位小数
<script type="text/javascript"> function _v(str){ var v4 = /^[+-]?\d+\.\d{0,4}$/i; return v4.test(str); } document.writeln("1.1111:"+_v("1.1111"));//true document.writeln("<br />"); document.writeln("1.111:"+_v("1.111"));//true document.writeln("<br />"); document.writeln("1.11:"+_v("1.11"));//true document.writeln("<br />"); document.writeln("1.1:"+_v("1.1"));//true document.writeln("<br />"); document.writeln("11.1:"+_v("11.1"));//true document.writeln("<br />"); document.writeln("111.1:"+_v("111.1"));//true document.writeln("<br />"); document.writeln("1111.1:"+_v("1111.1"));//true document.writeln("<br />"); document.writeln("1.11111:"+_v("1.11111"));//false document.writeln("<br />"); document.writeln("58.62596:"+_v("58.62596"));//false document.writeln("<br />"); document.writeln("58.6260:"+_v("58.6260"));//true document.writeln("<br />"); document.writeln("1.1.11.:"+_v("1.1.11."));//false </script>
1.1111:true 1.111:true 1.11:true 1.1:true 11.1:true 111.1:true 1111.1:true 1.11111:false 58.62596:false 58.6260:true 1.1.11.:false