function changeNumber(obj) { $(obj).val($(obj).val().trim()); if ($(obj).val() != "") { var number = $(obj).val().replace(/\,/g, ""); if (!isNaN(number)) { var num = parseFloat(number); $(obj).val(fmoney(num, 0)); $(obj).validatebox("validate"); } } } function fmoney(s, n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1]; t = ""; for (var i = 0; i < l.length; i++) { t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); } return t.split("").reverse().join("") + "." + r; } // 格式化金额 扩展 // number -- 金额; places -- 小数位数; symbol -- 币种符号; thousand -- 整数分隔符,默认为',' // decimal -- 小数分隔符,默认为. function formatMoney(number, places, symbol, thousand, decimal) { number = number || 0; places = !isNaN(places = Math.abs(places)) ? places : 2; symbol = symbol !== undefined ? symbol : "¥"; thousand = thousand || ","; decimal = decimal || "."; var negative = number < 0 ? "-" : "", i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "", j = (j = i.length) > 3 ? j % 3 : 0; return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : ""); } $.extend($.fn.validatebox.defaults.rules, { doubleLength : { validator : function(value, param) { var tmpValue = value.replace(/\,/g, ""); if (isNaN(tmpValue)) { return false; } var result = true; if (tmpValue.indexOf(".") > -1) { var valueInteger = tmpValue.substring(0, tmpValue.indexOf(".")); var valueDecimal = tmpValue.substring(tmpValue.indexOf(".") + 1); if (valueInteger.length > param[0]) { result = false; } if (valueDecimal.length > param[1]) { result = false; } } else { if (tmpValue.length > param[0]) { result = false; } } if(!$(this).is(":focus")){ var num = parseFloat(tmpValue); $(this).val(fmoney(num, 0)); } return result; }, message : '整数不能大于{0}位,小数不能大于{1}位.' } }); $.extend($.fn.validatebox.defaults.rules, { isNumber : { validator : function(value, param) { var tmpValue = value.replace(/\,/g, ""); var result = true; if (isNaN(tmpValue)) { result = false; } return result; }, message : '该字段为数字类型。' } });
转载请注明原文地址: https://www.6miu.com/read-74109.html