首页 > 网站 > WEB开发 > 正文

分享我常用的一些JS验证和函数

2024-04-27 14:16:23
字体:
来源:转载
供稿:网友

分享我常用的一些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则右边补0Number.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); } }                }            }        }    }    else {        rad = rad.FormatLen(len);    }    return numpart + "." + rad;}//检测字符串中是否有相同的元素split是字符串分隔符,如果指定了分隔符,则判断分隔符为分隔的字符串是否有重复,如果没指定则判断单个字符串是否有重复//有重复返回trueString.prototype.CompareElement = function (s) {    var str = this.toString();    if (s == undefined) {        for (var i = 0; i < str.length; i++) {            for (j = i + 1; j < str.length; j++) {                if (str.substr(i, 1) == str.substr(j, 1)) {                    return true;                }            }        }    }    else {        var strArr = str.split(s);        for (var i = 0; i < strArr.length; i++) {            for (var j = i + 1; j < strArr.length; j++) {                if (strArr[i] == strArr[j]) {                    return true;                }            }        }    }    return false;}String.prototype.replaceAll = function (str, tostr) {    oStr = this;    while (oStr.indexOf(str) > -1) {        oStr = oStr.replace(str, tostr);    }    return oStr;}Array.prototype.CompareElement = function () {    var strArr = this;    for (var i = 0; i < strArr.length; i++) {        for (var j = i + 1; j < strArr.length; j++) {            if (strArr[i] == strArr[j]) {                return true;            }        }    }    return false;}//字符串转组数,如果未指定分隔符s,则默认以,分隔分隔符,如果指定分隔符为空则将每个字符作为一个数组元素String.prototype.ToArray = function (s) {    if (s == undefined) { s = ","; }    var strArr = [];    strArr = this.split(s);    return strArr;}//将一个数组转换一个字符串,所有元素使用指定的分隔符连接,默认分隔为,Array.prototype.ToIDList = function (s) {    if (s == undefined) { s = ","; }    var list = "";    for (var i = 0; i < this.length; i++) {        list += (list == "" ? this[i] : s + "" + this[i]);    }    return list;}//获取指定元素的位置索引,元素不存在返回-1Array.prototype.GetIndex = function (s) {    var index = -1;    for (var i = 0; i < this.length; i++) {        if ((s + "") == this[i]) {            index = i;        }    }    return index;}//将指定元素从数组中删除Array.prototype.Remove = function (s) {    var list = "";    for (var i = 0; i < this.length; i++) {        if (s != this[i]) {            list += (list == "" ? this[i] : "," + this[i]);        }    }    return list.ToArray();}///将数组进行数字排序asc指定是否进行升序排序,可以为true或者false,未指定为升序Array.prototype.SortByNumber = function (asc) {    if (asc == undefined) { asc = true; }    if (asc) {        return this.sort(SortNumberAsc);    }    else {        return this.sort(SortNumberDesc);    }}Array.prototype.InArray = function (e) {    var IsIn = false;    for (var i = 0; i < this.length; i++) {        if (this[i] == (e + "")) {            IsIn = true;        }    }    return IsIn;}String.prototype.Trim = function (s) { return Trim(this, s); }String.prototype.LTrim = function (s) { return LTrim(this, s); }String.prototype.RTrim = function (s) { return RTrim(this, s); }//配合Array.SortByNumer使用,将数字进行数组降序排序function SortNumberDesc(a, b) {    return b - a;}//配合Array.SortByNumer使用,将数字进行数组升序排序function SortNumberAsc(a, b) {    return a - b;}//此处为独立函数function LTrim(str, s) {    if (s == undefined) { s = " "; }    if (str == s && s != " ") { return s; }    var i;    for (i = 0; i < str.length; i++) {        if (str.charAt(i) != s && str.charAt(i) != s) break;    }    str = str.substring(i, str.length);    return str;}function RTrim(str, s) {    var i;    if (str == s && s != " ") { return s; }    if (s == undefined) { s = " "; }    for (i = str.length - 1; i >= 0; i--) {        if (str.charAt(i) != s && str.charAt(i) != s) break;    }    str = str.substring(0, i + 1);    return str;}function Trim(str, s) {    return LTrim(RTrim(str, s), s);}///检测字符串是否是由中文,英文,数字以及下划线组成的function chkNickName(str) {    var pattern = /^[/w/u4e00-/u9fa5]+$/gi;    if (pattern.test(str)) {        return true;    }    return false;}//判断长度(长度不限为0)String.prototype.IsLen = function () {    var isRightFormat = false;    var minnum = arguments[0] ? arguments[0] : 0;    var maxnum = arguments[1] ? arguments[1] : 0;    isRightFormat = (minnum == 0 && maxnum == 0 ? true : (calculate_byte(this) >= minnum && calculate_byte(this) <= maxnum ? true : false));    return isRightFormat;}//验证字符串是否为字母+数字+_+-String.prototype.IsStr = function () {    var myReg = /^[0-9a-zA-Z/-/_]+$/;    if (myReg.test(this)) return true;    return false;}//验证用户名String.prototype.IsUsername = function () {    var myReg = /^[0-9a-zA-Z/-/_]{3,50}$/;    if (myReg.test(this)) return true;    return false;}//验证密码String.prototype.IsPassWord = function () {    var myReg = /^[0-9a-zA-Z`~!@#$%^&*()-_+=/{/}/[/]/;/:/"/'/?////]{6,}$/;    if (myReg.test(this)) return true;    return false;}//验证是否为字母String.prototype.IsEn = function () {    var myReg = /^[a-zA-Z]+$/;    if (myReg.test(this)) return true;    return false;}//验证是否为汉字String.prototype.IsCn = function () {    var myReg = /^[/u0391-/uFFE5]+$/;    if (myReg.test(this)) return true;    return false;}//验证E-mailString.prototype.IsEmail = function () {    var myReg = /([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)/;    if (myReg.test(this)) return true;    return false;}//验证MSNString.prototype.IsMSN = function () {    var myReg = /([/w-/.]+)@((/[[0-9]
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表