JS验证金额

 <script type="text/javascript">
        function ismoney(obj) {
            check(obj);
        }
        function check(obj) {
            var v = obj.value;
            if (v.indexOf(",") > -1) {
                //如果出现在最后一位和第一位不正确
                if (v.indexOf(",") == 0) {
                    alert("第一位不能出现,号")
                    changecolor(obj);
                    var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                    return false;
                }
                if (v.lastIndexOf(",") == (v.length - 1)) {
                    alert("最后一位不能出现,号");
                    changecolor(obj);
                    var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                    return false;
                }
                var tmp = v.split(",");
                for (var i = 1; i < tmp.length; i++) {
                    if (i == (tmp.length - 1)) {//最后一位
                        if (tmp[tmp.length - 1].indexOf(".") > -1) {
                            var la = tmp[tmp.length - 1].split(".")[0];
                            if (la == "" || la.length != 3) {
                                alert("小数点前的数字格式不正确,位数不为3个不能用逗号分隔");
                                changecolor(obj);
                                var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                                return false;
                            }
                        } else if (tmp[i] == "" || tmp[i].length != 3) {
                            alert("位数不为3个不能用逗号分隔");
                            changecolor(obj);
                            var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                            return false;
                        }
                    } else if (tmp[i] == "" || tmp[i].length != 3) {
                        alert("数字位数不正确");
                        changecolor(obj);
                        var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                        return false;
                    }
                }
                v = v.replace(/,/g, "");
            }
            var a = /^[0-9]*(\.[0-9]{1,2})?$/;
            if (!a.test(v)) {
                alert("格式不正确");
                changecolor(obj);
                var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
                obj.value = "";
//                clearTimeout(timeID);
                return false;
            } else {
                //alert("格式正确");
                return true;
            }
        }
        function changecolor(obj) {
            if (obj.type != "text")
                obj = document.getElementById(obj);
            //if(obj.type=="text")
            //    alert(obj.style.backgroundColor);
            if (obj.style.backgroundColor == "red") {
                obj.style.backgroundColor = ""
            } else {
                obj.style.backgroundColor = "red"
            }
        }
    </script>

  <asp:TextBox ID="txtZzkj" runat="server" onblur='ismoney(this)'></asp:TextBox>

posted @ 2014-05-07 10:15  盛开的雨季  阅读(3896)  评论(0编辑  收藏  举报