番茄的表单验证类代码修改版

2022-10-18,,,,

在经典论坛上发过一次,个人的项目中在后台处理时用到这个东西,对于简单的表单验证还是挺方便的。
因为不想让代码变得太臃肿,所以有很多不常用的功能就没有再添加了
对于我佛山人的意见就没有做修改了,为什么?因为我懒呗,哈哈
今天看到omeweb也修改了一个版本,做了许多修改,改得挺不错的,谢谢了。

源码在这里: 

//去除字符串两边的空格
string.prototype.trim = function () {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}
//检测字符串是否为空
string.prototype.isempty = function () {
    return !(/.?[^\s ]+/.test(this));
}
//检测值是否介于某两个指定的值之间
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('*');
    //字符串验证正则表达式
    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]$)/;
    //错误输出信息
    this.tip = new object();
    this.tip.unknow = '未找到的验证类型,无法执行验证。';
    this.tip.paramerror = '参数设置错误,无法执行验证。';
    this.tip.required = '不允许为空。';
    this.tip.english = '仅允许英文字符及下划线 (a-za-z0-9_)。';
    this.tip.chinese = '仅允许中文字符。';
    this.tip.number = '不是一个有效的数字。';
    this.tip.integer = '不是一个有效的整数。';
    this.tip.float = '不是一个有效的浮点数。';
    this.tip.date = '不是一个有效的日期格式。 (例如:2007-06-29)';
    this.tip.email = '不是一个有效的电子邮件格式。';
    this.tip.url = '不是一个有效的超链接格式。';
    this.tip.phone = '不是一个有效的电话号码。';
    this.tip.mobile = '不是一个有效的手机号码。';
    this.tip.ip = '不是一个有效的ip地址。';
    this.tip.zipcode = '不是一个有效的邮政编码。';
    this.tip.qq = '不是一个有效的qq号码。';
    this.tip.msn = '不是一个有效的msn帐户。';
    this.tip.idcard = '不是一个有效的身份证号码。';
    //获取控件名称
    this.getcontrolname = function ()
    {
        return this.element.getattribute('controlname') == null
               ? '指定控件的值'
               : this.element.getattribute('controlname');
    }
    //设定焦点
    this.setfocus = function (ele) {
        try {
            ele.focus();
        } catch (e){}
    }
    //设置边框颜色
    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;
        }
    }
    //输出错误反馈信息
    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);
    };
    //执行字符串验证
    this.validate = function () {
        var v = this.element.value;
        //验证是否允许非空
        var required = this.element.getattribute('required');
        if (required != null && v.isempty()) {
            this.feedback('required');
            return false;
        }
        //验证是否合法格式
        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;
            }
        }
        //执行数据验证
        var confirm = this.element.getattribute('confirm');
        if (confirm != null) {
            try {
                var data = eval('formobj.' + confirm + '.value');
                if (v != data) {
                    alert('两次输入的内容不一致,请重新输入。');
                    this.setbordercolor(this.element);
                    this.setfocus(this.element);
                    return false;
                }
            } catch (e) {
                this.feedback('paramerror');
                return false;
            }
        }
        //验证数字大小
        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 + ' 之
间的数字。');
                return false;
            }
        }
        //验证字符长度
        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 + ' 个字符
。');
                return false;
            }
        }
        return true;
    };
    //执行初始化操作
    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();
}

《番茄的表单验证类代码修改版.doc》

下载本文的Word格式文档,以方便收藏与打印。