JS 货币并格式化判断

来源:互联网 发布:矩阵tr表示什么 编辑:程序博客网 时间:2024/06/05 08:33
<!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> 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" type="text/javascript">
<!--
function checkMoney(obj){
var tempValue=obj.value.replace(/(^s+)|(s+$)/g,'').replace('','');
if(!tempValue){return}
if(/^-?d+(.d+)?$/.test(tempValue)){
obj.value
=""+parseFloat(tempValue).toFixed(2);
}
else{
alert(
'请输入合法的货币值!');
return
}

}

//-->
</script>
<input type="text" onchange="checkMoney(this)" size="20">
</body>
</html>