上善若水的博客

上善若水,水善利万物而不争

常用JS验证

百年项目用到的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;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012203867/article/details/53994731
个人分类: 前端
博主设置当前文章不允许评论。

常用js验证代码

2011年09月16日 61KB 下载

常用js验证

2013年08月20日 30KB 下载

开发常用JS验证整合

2011年04月20日 2KB 下载

JS验证表单大全

2008年07月09日 86KB 下载

JavaScript验证

2011年03月16日 562B 下载

JS验证表达式(各类完整版)

2018年04月18日 13KB 下载

js验证,js,java

2010年09月09日 1.43MB 下载

JS的一些常用正则表达式

2012年04月24日 2KB 下载

网站设计技巧

2008年04月30日 14KB 下载

extjs 自定义验证

2011年11月05日 60KB 下载

没有更多推荐了,返回首页

不良信息举报

常用JS验证

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭