关闭

常用JS验证

36人阅读 评论(0) 收藏 举报
分类:

百年项目用到的js验证,SE写的,没有注释,下次再整理

/*
 * 共通JS
 * Ver   Date            Author              Comment
 * -----+-----------+--------------------+-----
 * 1.00  2016/04/25      马**               新規作成
 */
function replaceAll(strvalue, strRepBy, strRepWith) {
    while (strvalue.indexOf(strRepBy) >= 0) {
        strvalue = strvalue.replace(strRepBy, strRepWith);
    }
    return strvalue;
}

function stop() {
    return false;
}
document.oncontextmenu = stop;

function replaceNotNumber(objValue) {
    var pattern = /[^0-9]/g;
    if (pattern.test(objValue.value)) {
        objValue.value = objValue.value.replace(pattern, "");
    }
}

String.prototype.format = function(args) {
    var result = this;
    if (arguments.length < 1) {
        return result;
    }

    var data = arguments;
    if (arguments.length == 1 && typeof (args) == "object") {

        data = args;
    }
    for (var key in data) {
        var value = data[key];
        if (undefined != value) {
            result = result.replace("{" + key + "}", value);
        }
    }
    return result;
}

function foramtString(str1, args) {

    var result = this;
    if (str1.length < 1) {
        return result;
    }

    var data = str1;
    if (str1.length == 1 && typeof (args) == "object") {

        data = args;
    }
    for (var key in data) {
        var value = data[key];
        if (undefined != value) {
            result = result.replace("{" + key + "}", value);
        }
    }
    return result;
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function ConvertoNumeric(strInput) {
    var strAfterChange = "";
    strInput = strInput + "";
    if (strInput != null && strInput != "") {
        strAfterChange = strInput.split(",").join("");
    }
    return strAfterChange;
}

function AddComma(strInput) {
    strInput = strInput + "";
    if (strInput == "" || strInput == null || escape(strInput) == "%20")
        return "";
    var temp1 = ConvertoNumeric(strInput);

    if (isNaN(temp1)) {
        return strInput;
    }
    temp1 = parseInt(temp1.split(".")[0], 10);
    temp1 = "" + Math.abs(temp1);
    temp1 = temp1.match(/./g).reverse().join("");
    temp1 = temp1.replace(/(\d{3})/g, "$1,");
    temp1 = temp1.match(/./g).reverse().join("").replace(/^,/, "");
    if (strInput.charAt(0) == '-')
        temp1 = "-" + temp1;
    if (!!strInput.split(".")[1])
        temp1 = temp1 + "." + strInput.split(".")[1];
    return temp1;
}

function showNumData(obj, spaceFlg, n, commaFlg) {

    var value = $.trim(obj.value);
    var inputStr = $.trim(obj.value);

    value = longCount(value, "1", "*", n);

    if (longCount(value, "1", "*", n) == longCount("0", "1", "*", n)
            || value == "") {
        value = "";
    }
    if (value == "") {
        if (spaceFlg == "yes" && value == "" && inputStr == "") {
            value = "";
        } else if (spaceFlg == "yes" && value == "" && inputStr != "") {
            value = longCount("0", "1", "*", n);
        } else {
            value = longCount("0", "1", "*", n);
        }
    } else {
        if (commaFlg == "yes") {
            value = longCount(value, "1", "*", n);
            value = AddComma(value);
        }
    }

    obj.value = value;
}

function longCount(str1, str2, type, n) {

    if (str1 == null || str1 == "" || str1.length <= 0) {
        return "";
    }

    if (str2 == null || str2 == "" || str2.length <= 0) {
        return "";
    }

    if (type == null || type == "" || type.length <= 0) {
        return "";
    }

    var pra1 = str1 * 1;
    var pra2 = str2 * 1;
    var value;

    if (type == "+") {
        value = pra1 + pra2;

    } else if (type == "-") {
        value = pra1 - pra2;

    } else if (type == "*") {
        value = pra1 * pra2;

    } else if (type == "/") {

        value = pra1 / pra2;
    }
    if (typeof n != 'undefined' && n != null && n == 0) {
        value = Math.round(value);
        if (isNaN(value)) {
            value = "";
        }
        return value;
    }
    if (typeof n != 'undefined' && n != null && n != "") {
        var f_round_pos = n + 1;
        var f_str_point = value.toString().lastIndexOf(".");
        if (f_str_point == -1
                || f_str_point > value.toString().length - f_round_pos) {
            value = value;
        } else {
            var pos = new Number(f_round_pos) - 1;
            var work_num = Math.round(value * Math.pow(10, pos));
            work_num = work_num / Math.pow(10, pos);
            value = work_num;
        }

        value = value.toFixed(n);
    }
    if (isNaN(value)) {
        value = "";
    }
    return value;
}

function editNumData(obj) {
    var strInput = $.trim(obj.value);
    var strAfterChange = "";
    strAfterChange = ConvertoNumeric(strInput);
    obj.value = longCount(strAfterChange, "1", "*");
    var textObj = obj.createTextRange();
    textObj.moveStart("character", obj.value.length);
    textObj.collapse(true);
    textObj.select();
    document.getElementById(obj.id).select(); 
}

var dateFormatString = "YYYY/MM/DD";

function addZero(n) {
    if (n == null || n == "") {
        return n;
    }
    n = n.toString();
    if (n.length < 2) {
        return "0" + n.toString();
    } else {
        return n.toString();
    }
}

function CheckYyyymmdd(sDate, type) {
    var chkData = sDate;

    if (chkData == null || chkData == "") {
        return true;
    }
    if (dateFormatString == null || dateFormatString == "") {
        return false;
    }
    if (chkData.length != dateFormatString.length) {
        return false;
    }

    var dateString = dateFormatString.toUpperCase();
    var year_position = dateString.indexOf("YYYY");
    var month_position = dateString.indexOf("MM");
    var day_position = dateString.indexOf("DD");

    if (year_position == -1 || month_position == -1 || day_position == -1) {
        return false;
    }
    var year = 1 * chkData.substr(year_position, 4);
    var month = 1 * chkData.substr(month_position, 2) - 1;
    var day = 1 * chkData.substr(day_position, 2);

    var newDate = new Date(year, month, day);
    var newYear = 1 * newDate.getFullYear();
    var newMonth = 1 * newDate.getMonth();
    var newDay = 1 * newDate.getDate();

    var cDate = new Date();

    if (type && cDate.getFullYear() - newYear > 2) {
        return false;
    }

    if (newYear != year) {
        return false;
    }
    if (newMonth != month) {
        return false;
    }
    if (newDay != day) {
        return false;
    }
    var newDateString = dateFormatString.replace("YYYY", newYear);
    newDateString = newDateString.replace("MM", addZero(newMonth + 1));
    newDateString = newDateString.replace("DD", addZero(newDay));
    if (newDateString != chkData) {
        return false;
    } else {
        return true;
    }
}

function dateCheckStart(obj) {
    var bol = CheckYyyymmdd(obj.value, true);

    if (!bol) {
        obj.value = "";
        obj.focus();

        return false;
    }

    return true;
}

function dateCheck(obj) {
    var bol = CheckYyyymmdd(obj.value, false);

    if (!bol) {
        obj.value = "";
        obj.focus();

        return false;
    }

    return true;
}

function controlData(element) {
    var sp = "/";
    var changeLength = 0;
    var strString = dateFormatString;
    strString = strString.replace("YYYY", "1");
    strString = strString.replace("MM", "1");
    strString = strString.replace("DD", "1");
    var first;
    var second;
    for (var i = 0; i < strString.length; i++) {
        if (window.isNaN(strString.charAt(i) * 1) || strString.charAt(i) == " ") {
            sp = strString.charAt(i) + "";
            break;
        }
    }
    strString = dateFormatString;
    first = strString.indexOf(sp);
    if (first < 0) {
        changeLength++;
    }
    strString = strString.replace(sp, "");
    second = strString.indexOf(sp);
    if (first >= 0) {
        second += 1;
    }
    if (second < 0) {
        changeLength++;
    }
    var svalue = element.value;
    var nc = event.keyCode;
    element.maxLength = 10 - changeLength;
    if ((nc >= 48) && (nc <= 57)) {
        if (svalue.length == first || svalue.length == second) {
            element.value = svalue + sp;
        }
    } else {
        event.keyCode = 0;
        return;
    }
}

function validateNumber(obj) {
    var reg = new RegExp("^[0-9]*$");

    if (!reg.test(obj.value)) {
        return false;
    }

    return true;
}

function validateEMail(str) {
    var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/
    if (reg.test(str.value)) {
        return false;
    }

    return true;
}

function validateEnNumber(obj) {
    var reg = new RegExp("^[A-Za-z0-9]+$");

    if (!reg.test(obj.value)) {
        return false;
    }

    return true;
}

function checkValueLength(obj, len) {

    if (obj.value.length > len) {

        return false;
    }

    return true;
}

function checkValueIsNull(obj) {

    if (obj.value == "" || obj.value == null) {

        return false;
    }

    return true;
}

function getNowDate() {

    var sysDate = new Date();

    var year = sysDate.getFullYear();

    var month = formatStr(sysDate.getMonth() + 1, 2);

    var date = formatStr(sysDate.getDate(), 2);

    return year + "/" + month + "/" + date;
}

function formatStr(str, len) {

    var strLen = str.toString().length;

    while (strLen < len) {

        str = "0" + str;

        strLen++;

    }

    return str;
}

function getUrlParm(name) {
    var reg = new RegExp("(^|&)" + name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return   null;
}

// 小数的判断(*.*)
function numberCheck(obj, smaLen, intLen) {
    if (!isNaN(obj.value)) {
        var dot = obj.value.indexOf(".");
        if (dot != -1) {
            var dotCnt = obj.value.substring(dot + 1, obj.length);
            if (dotCnt.length > smaLen) {
                return false;
            }
            var intCnt = obj.value.substring(0, dot);
            if (intCnt.length > intLen) {
                return false;
            }
        } else if (obj.value.length > intLen) {
            return false;
        }
    } else {
        return false;
    }
    return true;
}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:93次
    • 积分:19
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档