常用的JS验证和函数汇总

下面是我常用一些JS验证函数,有一些验证我直接写到了对象的属性里面了,可以直接通过对象.方法来调用

复制代码 代码如下:


//浮点数除法运算
function fdiv(a, b, n) {
    if (n == undefined) { n = 2; }
    var t1 = 0, t2 = 0, r1, r2;
    try { t1 = a.toString().split(".")[1].length } catch (e) { }
    try { t2 = b.toString().split(".")[1].length } catch (e) { }
    with (Math) {
        r1 = Number(a.toString().replace(".", ""));
        r2 = Number(b.toString().replace(".", ""));
        return ((r1 / r2) * pow(10, t2 - t1)).toFixed(n);
    }
}

复制代码 代码如下:


//浮点数乘法运算
function fmul(a, b, n) {
    if (n == undefined) { n = 2; }
    var m = 0, s1 = a.toString(), s2 = b.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return (Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)).toFixed(n);
}

复制代码 代码如下:


//浮点数加法运算
function fadd(a, b, n) {
    if (n == undefined) { n = 2; }
    var r1, r2, m;
    try { r1 = a.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = b.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return ((a * m + b * m) / m).toFixed(n);
}

复制代码 代码如下:


//浮点数减法运算
function fsub(a, b, n) {
    if (n == undefined) { n = 2; }
    var r1, r2, m;
    try { r1 = a.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = b.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
    //n = (r1 >= r2) ? r1 : r2;
    return ((a * m - b * m) / m).toFixed(n);
}
Number.prototype.add = function (arg) {
    return fadd(this, arg);
}
Number.prototype.subs = function (arg) {
    return fsub(this, arg);
}
Number.prototype.mul = function (arg) {
    return fmul(this, arg);
}
Number.prototype.div = function (arg) {
    return fdiv(this, arg);
}

复制代码 代码如下:


///格式化数字位数,不足位数默认左边补0,如果指定了参数2并且参数2的值为1则右边补0
Number.prototype.FormatLen = function (len, direct) {
    var d = parseInt(direct);
    if (isNaN(d)) { d = 0; }
    var num = this.toString();
    if (num.length < len) {
        for (var i = num.length; i < len; i++) {
            if (d == 0) {
                num = "0" + num;
            }
            else {
                num += "0";
            }
        }
    }
    return num;
}

复制代码 代码如下:


//格式化小数点数位,可以指定小数位数,是否四舍五入等参数
Number.prototype.FormatRadix = function (len, IsRound) {
    var num = this.toString();
    var numArr = num.split('.');
    var rad = 0;
    var numpart = parseInt(numArr[0]);
    if (numArr.length >= 2) {
        if (numArr[1].length < len) {
            rad = parseInt(numArr[1]).FormatLen(len, 1);
        }
        else {
            if (numArr[1].length == len) {
                rad = numArr[1];
            }
            else {
                rad = numArr[1].substr(0, len);
                if (IsRound) {
                    var d = parseInt(numArr[1].substr(len, 1));
                    if (d >= 5) { rad += 1; if (rad.toString().length > len) { numpart += 1; rad = rad.toString().substr(1, len); } }
                }
            }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxfxf.html