Jquery Money 验证,转换成千分位

 function Convert(amtStr) {
        var a, renum = '';
        var j = 0;
        var a1 = '', a2 = '', a3 = '';
        var tes = /^-/;
        a = amtStr.replace(/,/g, "");
        a = a.replace(/[^-\.,0-9]/g, ""); //删除无效字符
        a = a.replace(/(^\s*)|(\s*$)/g, ""); //trim
        if (tes.test(a)) a1 = '-';
        else a1 = '';
        a = a.replace(/-/g, "");
        if (a != "0" && a.substr(0, 2) != "0.") a = a.replace(/^0*/g, "");
        j = a.indexOf('.'); if (j < 0) j = a.length; a2 = a.substr(0, j); a3 = a.substr(j); j = 0;
        for (i = a2.length; i > 3; i = i - 3) {
            renum = "," + a2.substr(i - 3, 3) + renum;
            j++;
        }
        renum = a1 + a2.substr(0, a2.length - j * 3) + renum + a3;

        return renum;
    }
 /******转换金钱*****/
    function ConvertMoney() {
        $("[money]").each(function () {
            var str = $(this).val();
            $(this).keyup(function (evt) {
                str = $(this).val();
                evt = (evt) ? evt : ((window.event) ? window.event : "") //兼容IE和Firefox获得keyBoardEvent对象
                var key = evt.keyCode ? evt.keyCode : evt.which; //兼容IE和Firefox获得keyBoardEvent对象的键值
                if (key >= 33 && key <= 40) {
                    evt.preventDefault();
                    evt.returnValue = false;
                    return false;
                }
                $(this).val(Convert(str));
            });


            if (str != "") {
                $(this).val(Convert(str));
            }
        });
    }
  $(function () {

ConvertMoney();


});

先引入上面的代码 ,然后在你的textbox上加上一个money属性,值为任意值
然后就可心验证所有的金钱了

posted on 2011-07-28 14:41  HelloHongfu  阅读(2000)  评论(0编辑  收藏  举报

导航