相信大家看代码后再添加一些针对其他格式的验证也不是太麻烦的事情
对于各种提示效果需要修改 feedback 方法就可以实现,因为不想让代码变得太臃肿,所以有很多不常用的功能就没有再添加了
主要目的提供大家参考与分享,也希望能够根据大家的意见使其变得更加完善
//去除字符串两边的空格
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 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();
}
密码 | password | |
密码确认 | confirm | |
英文 | english | 4~10个字符 |
中文 | chinese | 非空 |
数字 | number | 大于10,小于100 |
整数 | integer | |
浮点数 | float | |
日期 | date | |
邮件 | ||
网址 | url | |
电话 | phone | |
手机 | mobile | |
ip地址 | ip | |
邮编 | zipcode | |
qq号码 | ||
msn | msn | |
身份证 | idcard | |
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]
1.".3"这样也是算浮点数的
2.this.reg和this.tip可以用
this.reg = {englisth : /^[a-za-z0-9_\-]+$/, chinese : /^[\u0391-\uffe5]+$/}
这种形式,省点代码
3.日期只验证格式没有验证合法性
btw:controlname的做法不错