zoukankan      html  css  js  c++  java
  • html 根据配置项统一检查文本框数据规范

        <div>
            中文名:<input id="txtName" type="text" /><br />
            身份证号:<input id="txtPersonID" type="text" /><br />
            <input type="button" value="测试" onclick="chk();" />
        </div>
        <script type="text/javascript">
            //input text文本录入框数据统一检查
            //items:检查配置项,传入对象数组
            //配置项键说明:ID:录入控件ID; ColumnName:录入内容字段名,MustInput:是否必填,Check:检查方式[1:如果是值格式固定的检查用正则表达式,这里传入正则表达式的字符串 2:其它特殊值格式检查,可以自定义检查函数,这里传入检查函数(参数为文本框的值,返回值bool类型)],CheckErrInfo:检查失败提示信息
            function checkInput(items) {
                var info = "";
                //检查参数类型
                if (typeof items == "object" && typeof items.length == "number") {
                    for (var i = 0; i < items.length; i++) {
                        if (typeof items[i].ID != 'string' || typeof items[i].ColumnName != 'string' || typeof items[i].MustInput != 'boolean' || (typeof items[i].Check != 'string' && typeof items[i].Check != 'function') || typeof items[i].CheckErrInfo != 'string') continue;
    
                        //取值
                        var value = document.getElementById(items[i].ID).value;
                        //必填项检查
                        if (items[i].MustInput == true) {
                            if (value == '') {
                                info += items[i].ColumnName + "必填;";
                            }
                        }
                        else {
                            continue; //不是文本框,
                        }
                        //检查数据规范
                        if (value != '') {
                            if (typeof items[i].Check == 'string') {
                                if (!regValid(items[i].Check, value)) {
                                    info += items[i].ColumnName + items[i].CheckErrInfo + ";";
                                }
                            }
                            else if (typeof items[i].Check == 'function') {
                                if (!items[i].Check(value)) {
                                    info += items[i].ColumnName + items[i].CheckErrInfo + ";";
                                }
                            }
                        }
                    }
                }
                return info;
            }
    
            function regValid(regStr, value) {
                var rtn = false;
                if (typeof regStr == "string" && regStr != '' && typeof value == 'string') {
                    var re = new RegExp(regStr);
                    rtn = re.test(value);
                }
                return rtn;
            }
    
            //测试
            function chk() {
                var chkItems = new Array();
                //正则表达式检查中文名称
                chkItems.push({ "ID": "txtName", "ColumnName": "名称", "Check": "^[u4E00-u9FA5uF900-uFA2D]{2,4}$", "MustInput": true, "CheckErrInfo": "只能2-4位汉字" });
                //也可以自定义函数检查,这里用chkPersonId
                chkItems.push({ "ID": "txtPersonID", "ColumnName": "身份证号", "Check": chkPersonId, "MustInput": true, "CheckErrInfo": "只能17位数字+1位数字或字母" });
                var msg = checkInput(chkItems);
                if (msg != '') alert(msg);
            }
    
            //自定义检查函数
            function chkPersonId(personId) {
                return regValid("^[0-9]{17}[0-9a-zA-Z]{1}$", personId);
            }
    
        </script>
  • 相关阅读:
    中考 2020 游记
    CodeChef 2020 July Long Challenge 题解
    GDOI2020 游记
    AtCoder Grand Contest 044 题解
    ISIJ2020 不知道算不算游记
    WC2020 拿铁记
    UOJ Round 19 题解
    本博客采用 CC BY-NC-SA 4.0 进行许可
    [算法模版]回文树
    AddressSanitizer
  • 原文地址:https://www.cnblogs.com/cy2011/p/7417503.html
Copyright © 2011-2022 走看看