源碼在這里:
//去除字符串兩邊的空格
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 = '未找到的驗證類型,無法執(zhí)行驗證。';
this.tip.paramError = '參數(shù)設置錯誤,無法執(zhí)行驗證。';
this.tip.required = '不允許為空。';
this.tip.english = '僅允許英文字符及下劃線 (a-zA-Z0-9_)。';
this.tip.chinese = '僅允許中文字符。';
this.tip.number = '不是一個有效的數(shù)字。';
this.tip.integer = '不是一個有效的整數(shù)。';
this.tip.float = '不是一個有效的浮點數(shù)。';
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);
};
//執(zhí)行字符串驗證
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;
}
}
//執(zhí)行數(shù)據(jù)驗證
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;
}
}
//驗證數(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;
}
}
//驗證字符長度
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;
};
//執(zhí)行初始化操作
this.init = function () {
for (var i=0; i
this.allTags[i].tagName.toUpperCase() == 'SELECT' ||
this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
{
this.element = allTags[i];
if (!this.validate())
return false;
}
}
};
return this.init();
}
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com