对Textbox的值转换为带千位符和小数的Decimal字符串
以下Function可以用于textbox的KeyUp事件:
2014-06-06 发现旧版IE不支持selectionStart还有字符串的"[]"索引获取值, 已经修复这个bug.
2014-06-10 修复上一次修复遗留的IE的bug.
/* Validate the textbox value is decimal. */ var numberChars = "1234567890"; function isDecimal(item) { var obj = $(item); if (obj.length > 0) { if ($(obj).val() != null && typeof ($(obj).val()) != 'undefined') { var str = $(obj).val().toString(); if (str != "") { //记录初始光标位置 var selectionStart = item.selectionStart; //兼容IE if (typeof (item.createTextRange) != "undefined") { item.focus(); var r = document.selection.createRange(), tr = item.createTextRange(); tr.setEndPoint('endtoend', r); item.selectionStart = tr.text.length - r.text.length; item.selectionEnd = tr.text.length; selectionStart = item.selectionStart; } //用于判断千位符的数量变化 var pren = str.split(',').length - 1; //合法Decimal格式字符串, 可以含千位符(,号) var pattern = '^-?0(\\.\\d+)?$|^-?[1-9]\\d*(\\.\\d+)?$|^-?([1-9][0-9]{0,2},)(\\d{3},)*(\\d{3})(\\.\\d+)?$'; //合法字符集, 不包括,号 var pattern2 = '^[0-9\.-]*$'; var reg = new RegExp(pattern, 'g'); var reg2 = new RegExp(pattern2, 'g'); //转换之前, 去除,号 var temp = str.replace(/,/, ""); while (temp.indexOf(',') >= 0) { temp = temp.replace(/,/, ""); } var nstr = ''; if (reg2.test(temp)) { //除(/)数和模(%)数 var k1 = 0, k2 = 0; //转换开始和结束位置 var start = 0, end = 0; //千位符(,号) var pp = ','; //计数(3的倍数) var p = 0; //判断前置的非数字符号(这里是-号) for (; start < temp.length; start++) { if (numberChars.indexOf(temp.substring(start, start + 1)) >= 0) { break; } nstr = nstr.concat(temp.substring(start, start + 1)); } //小数符号(.号)的位置 var pIndex = temp.indexOf('.'); //存在小数符(.号), 即以它的位置为结束位置, 否则以字符串结尾为结束位置 if (pIndex >= 0) { end = pIndex; } else { end = temp.length; } k2 = (end - start) % 3; k1 = parseInt((end - start) / 3); for (var i = 0; i < k2; i++) { nstr = nstr.concat(temp.substring(start + i, start + i + 1)); } if (k1 > 0 && k2 > 0) { nstr = nstr.concat(pp); } for (var i = k2 + start; i < end; i++) { nstr = nstr.concat(temp.substring(i, i + 1)); p++; if (p == 3 && i + 1 != end) { p = 0; nstr = nstr.concat(pp); } } for (var i = end; i < temp.length; i++) { nstr = nstr.concat(temp.substring(i, i + 1)); } } else { nstr = str; } if (!reg.test(nstr)) { //去除不合法符号(包括手动输入的,号) if (nstr.match(/[^0-9,\.-]/g) != null) { var matchLength = nstr.match(/[^0-9,\.-]/g).length; if (matchLength > 0) { var tempLength = nstr.length; nstr = nstr.replace(/[^0-9,\.-]/g, ''); $(item).val(nstr); selectionStart = selectionStart - tempLength + nstr.length; } } } $(item).val(nstr); if (str != nstr) { var tpren = nstr.split(',').length - 1; if (pren != tpren) { //根据千位符的数量变化, 控制光标位置 item.selectionStart = selectionStart + tpren - pren; item.selectionEnd = selectionStart + tpren - pren; } else { item.selectionStart = selectionStart; item.selectionEnd = selectionStart; } } //兼容IE if (typeof (item.createTextRange) != "undefined") { var range = item.createTextRange(); range.collapse(true); range.moveEnd('character', item.selectionEnd); range.moveStart('character', item.selectionStart); range.select(); } return true; } } } return false; }