求一数据校验格式:小数位最多输入两位小数
求一数据校验格式:
小数位最多输入两位小数(整个框也可以直接输入:“.08,.88”类型的数字):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
</HEAD>
<BODY>
<script language=javascript>
function check(e){
var num=e.value;
re=/^([-]?)([0-9]+)((.[0-9]{1,2})?)$/;有一个缺陷可以输入类似010的数
if(re.test(num))
{
alert( "正确 ");
}
else
{
alert( "请检查数据格式,且小数点后只能2位! ");
e.value= ' ';
}
}
</script>
<div align= "center ">
<input type= "text " name=num onblur=check(this)>
</div>
</BODY>
</HTML>
------解决方案-------------------- <input type= "text " name=num onblur=check(this)>
<script language= "javascript ">
var txt =document.num.value;
var indexD = txt.indexOf( '. ');
var aftStr = txt.substring(indexD +1,txt.length);
if(aftStr.length> 2 && indexD > 0)
{
alert( "小数位最多两位! ");
return false;
}
return true;
}
</script>
------解决方案--------------------var reg = /^[-]?((?!0)\d*|0)(\.\d{1,2})?$/gi;
------解决方案--------------------function Checkdecimal(decimal,thisname){ //判断 线路长度 小数输入 限两位小数
var thisdecimal = decimal;
var re = /^\d*\.?\d{0,2}$/;
if(thisdecimal!= " "){
if (re.exec(thisdecimal) != null){
//alert( "right ");
}
else
{alert( "线路长度限输入数字且最多2位小数! ");
document.forms[0].elements[ " "+thisname+ " "].focus();
}
}
}