一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

番茄的表單驗(yàn)證類代碼修改版

在經(jīng)典論壇上發(fā)過一次,個(gè)人的項(xiàng)目中在后臺(tái)處理時(shí)用到這個(gè)東西,對(duì)于簡單的表單驗(yàn)證還是挺方便的。
因?yàn)椴幌胱尨a變得太臃腫,所以有很多不常用的功能就沒有再添加了
對(duì)于我佛山人的意見就沒有做修改了,為什么?因?yàn)槲覒袉h,哈哈
今天看到omeweb也修改了一個(gè)版本,做了許多修改,改得挺不錯(cuò)的,謝謝了。

源碼在這里: 

//去除字符串兩邊的空格
String.prototype.trim = function () {
    return this.replace(/(^/s+)|(/s+$)/g, "");
}
//檢測(cè)字符串是否為空
String.prototype.isEmpty = function () {
    return !(/.?[^/s ]+/.test(this));
}
//檢測(cè)值是否介于某兩個(gè)指定的值之間
String.prototype.isBetween = function (val, min, max) {
    return isNaN(val) == false && val >= min && val <= max;
}
//獲取最大值或最小值
String.prototype.getBetweenVal = function (what) {
    var val = this.split(',');
    var min = val[0];
    var max = val[1] == null ? val[0] : val[1];
    if (parseInt(min) > parseInt(max)) {
        min = max;
        max = val[0];
    }
    return what == 'min' ? (isNaN(min) ? null : min) : (isNaN(max) ? null : max);
}
var validator = function (formObj) {
    this.allTags = formObj.getElementsByTagName('*');
    //字符串驗(yàn)證正則表達(dá)式
    this.reg = new Object();
    this.reg.english = /^[a-zA-Z0-9_/-]+$/;
    this.reg.chinese = /^[/u0391-/uFFE5]+$/;
    this.reg.number = /^[-/+]?/d+(/./d+)?$/;
    this.reg.integer = /^[-/+]?/d+$/;
    this.reg.float = /^[-/+]?/d+(/./d+)?$/;
    this.reg.date = /^(/d{4})(-|//)(/d{1,2})/2(/d{1,2})$/;
    this.reg.email = /^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/;
    this.reg.url = /^(((ht|f)tp(s?))/:////)[a-zA-Z0-9]+/.[a-zA-Z0-9]+[//=/?%/-&_~`@[/]
/':+!]*([^<>/"/"])*$/;
    this.reg.phone = /^((/(/d{2,3}/))|(/d{3}/-))?(/(0/d{2,3}/)|0/d{2,3}-)?[1-9]/d{6,7}(/-/d
{1,4})?$/;
    this.reg.mobile = /^((/(/d{2,3}/))|(/d{3}/-))?((13/d{9})|(159/d{8}))$/;
    this.reg.ip = /^(0|[1-9]/d?|[0-1]/d{2}|2[0-4]/d|25[0-5]).(0|[1-9]/d?|[0-1]/d{2}|2[0-4]
/d|25[0-5]).(0|[1-9]/d?|[0-1]/d{2}|2[0-4]/d|25[0-5]).(0|[1-9]/d?|[0-1]/d{2}|2[0-4]/d|25[0-
5])$/;
    this.reg.zipcode = /^[1-9]/d{5}$/;
    this.reg.qq = /^[1-9]/d{4,10}$/;
    this.reg.msn = /^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$/;
    this.reg.idcard = /(^/d{15}$)|(^/d{17}[0-9Xx]$)/;
    //錯(cuò)誤輸出信息
    this.tip = new Object();
    this.tip.unknow = '未找到的驗(yàn)證類型,無法執(zhí)行驗(yàn)證。';
    this.tip.paramError = '參數(shù)設(shè)置錯(cuò)誤,無法執(zhí)行驗(yàn)證。';
    this.tip.required = '不允許為空。';
    this.tip.english = '僅允許英文字符及下劃線 (a-zA-Z0-9_)。';
    this.tip.chinese = '僅允許中文字符。';
    this.tip.number = '不是一個(gè)有效的數(shù)字。';
    this.tip.integer = '不是一個(gè)有效的整數(shù)。';
    this.tip.float = '不是一個(gè)有效的浮點(diǎn)數(shù)。';
    this.tip.date = '不是一個(gè)有效的日期格式。 (例如:2007-06-29)';
    this.tip.email = '不是一個(gè)有效的電子郵件格式。';
    this.tip.url = '不是一個(gè)有效的超鏈接格式。';
    this.tip.phone = '不是一個(gè)有效的電話號(hào)碼。';
    this.tip.mobile = '不是一個(gè)有效的手機(jī)號(hào)碼。';
    this.tip.ip = '不是一個(gè)有效的IP地址。';
    this.tip.zipcode = '不是一個(gè)有效的郵政編碼。';
    this.tip.qq = '不是一個(gè)有效的QQ號(hào)碼。';
    this.tip.msn = '不是一個(gè)有效的MSN帳戶。';
    this.tip.idcard = '不是一個(gè)有效的身份證號(hào)碼。';
    //獲取控件名稱
    this.getControlName = function ()
    {
        return this.element.getAttribute('controlName') == null
               ? '指定控件的值'
               : this.element.getAttribute('controlName');
    }
    //設(shè)定焦點(diǎn)
    this.setFocus = function (ele) {
        try {
            ele.focus();
        } catch (e){}
    }
    //設(shè)置邊框顏色
    this.setBorderColor = function (ele) {
        var borderColor = ele.currentStyle ?
                          ele.currentStyle.borderColor :
                          document.defaultView.getComputedStyle(ele, null)['borderColor'];
        ele.style.borderColor = '#ff9900';
        ele.onkeyup = function () {
            this.style.borderColor = borderColor;
        }
    }
    //輸出錯(cuò)誤反饋信息
    this.feedback = function (type) {
        try {
            var msg = eval('this.tip.' + type) == undefined ?
                      type :
                      this.getControlName() + eval('this.tip.' + type);
        } catch (e) {
            msg = type;
        }
        this.setBorderColor(this.element);
        alert(msg);
        this.setFocus(this.element);
    };
    //執(zhí)行字符串驗(yàn)證
    this.validate = function () {
        var v = this.element.value;
        //驗(yàn)證是否允許非空
        var required = this.element.getAttribute('required');
        if (required != null && v.isEmpty()) {
            this.feedback('required');
            return false;
        }
        //驗(yàn)證是否合法格式
        var dataType = this.element.getAttribute('dataType');
        if (!v.isEmpty() && dataType != null &&  dataType.toLowerCase() != 'password') {
            dataType = dataType.toLowerCase();
            try {
                if (!(eval('this.reg.' + dataType)).test(v)) {
                    this.feedback(dataType);
                    return false;
                }
            } catch(e) {
                this.feedback('unknow');
                return false;
            }
        }
        //執(zhí)行數(shù)據(jù)驗(yàn)證
        var confirm = this.element.getAttribute('confirm');
        if (confirm != null) {
            try {
                var data = eval('formObj.' + confirm + '.value');
                if (v != data) {
                    alert('兩次輸入的內(nèi)容不一致,請(qǐng)重新輸入。');
                    this.setBorderColor(this.element);
                    this.setFocus(this.element);
                    return false;
                }
            } catch (e) {
                this.feedback('paramError');
                return false;
            }
        }
        //驗(yàn)證數(shù)字大小
        var dataBetween = this.element.getAttribute('dataBetween');
        if (!v.isEmpty() && dataBetween != null) {
            var min = dataBetween.getBetweenVal('min');
            var max = dataBetween.getBetweenVal('max');
            if (min == null || max == null) {
                this.feedback('paramError');
                return false;
            }
            if (!v.isBetween(v.trim(), min, max)) {
                this.feedback(this.getControlName() + '必須是介于 ' + min + '-' + max + ' 之
間的數(shù)字。');
                return false;
            }
        }
        //驗(yàn)證字符長度
        var dataLength = this.element.getAttribute('dataLength');
        if (!v.isEmpty() && dataLength != null) {
            var min = dataLength.getBetweenVal('min');
            var max = dataLength.getBetweenVal('max');
            if (min == null || max == null) {
                this.feedback('paramError');
                return false;
            }
            if (!v.isBetween(v.trim().length, min, max)) {
                this.feedback(this.getControlName() + '必須是 ' + min + '-' + max + ' 個(gè)字符
。');
                return false;
            }
        }
        return true;
    };
    //執(zhí)行初始化操作
    this.init = function () {
        for (var i=0; i<this.allTags.length; i++) {
            if (this.allTags[i].tagName.toUpperCase() == 'INPUT' ||
                this.allTags[i].tagName.toUpperCase() == 'SELECT' ||
                this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
            {
                this.element = allTags[i];
                if (!this.validate())
                    return false;
            }
        }
    };
    return this.init();
}

JavaScript技術(shù)番茄的表單驗(yàn)證類代碼修改版,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久99国产精品二区不卡 | 一区二区3区免费视频 | 97香蕉超级碰碰碰久久兔费 | 六月婷婷激情 | 青青在线精品 | 久久免费视屏 | 亚洲国产成人超福利久久精品 | www.激情五月| 婷婷丁香五月中文字幕 | 九月色婷婷 | 99视频精品全部国产盗摄视频 | 99精品视频一区在线视频免费观看 | 91短视频在线观看免费最新91 | 久久一区二区精品 | 成人免费xxx在线观看 | 91精品在线免费视频 | 羞羞色男人的天堂伊人久久 | 91精品国产麻豆91久久久久久 | 欧美一区二区三区gg高清影视 | 亚洲视频在线一区二区三区 | 成人青草亚洲国产 | 亚洲一级毛片视频 | 日本精品久久久久护士 | 四虎中文 | 久久亚洲精品视频 | 国产九色在线 | 国产精品98福利小视频 | 精品国产自在现线看久久 | 天堂在线www天堂中文在线 | 精品成人一区二区 | 久久久久777777人人人视频 | 小泽玛利亚一区二区三区免费 | 91精品一区二区三区在线 | 思思久久99热这里只有精品66 | 日韩一区二区三区视频在线观看 | 国产视频大全 | 最近中文字幕完先锋资源 | 中文字幕综合久久久久 | 中文字幕日产每天更新40 | 激情综合网五月 | 国产欧美激情一区二区三区-老狼 |