JavaScript之保留两位小数

 1         if (!isNumeric(fm.ChangeFee.value)) {
 2             alert("请输入正确的变更费用");
 3             document.getElementsByName('ChangeFee')[0].style.borderColor = "#f64f4f";
 4             return false;
 5         }
 6 
 7         var string = fm.ChangeFee.value.toString();
 8         var index = string.indexOf(".");
 9         if (index > -1) {
10             var pointB = parseInt(string.substring(0, index));
11             var pointA = parseInt(string.substring(index + 1, string.length));
12             if (index > 10 || getStrByte(pointA.toString()) > 2) {
13                 alert("变更费用长度限制为10位整数,2位小数");
14                 document.getElementsByName('ChangeFee')[0].style.borderColor = "#f64f4f";
15                 return false;
16             }
17         } else if (getStrByte(string) > 10) {
18             alert("变更费用字符长度限制为10位整数,2位小数");
19             document.getElementsByName('ChangeFee')[0].style.borderColor = "#f64f4f";
20             return false;
21         }

1 function isNumeric(sNumer) {
2     var RegChkExp = /^(\+?)(\-?)(\d+)(\.\d+)?$/;
3     return RegChkExp.test(sNumer);
4 }

posted on 2019-12-15 18:37  ~码铃薯~  阅读(302)  评论(0编辑  收藏  举报

导航