番茄的表單驗(yàn)證類代碼修改版
更新時(shí)間:2008年07月18日 08:48:43 作者:
今天看到omeweb也修改了一個(gè)版本,做了許多修改,改得挺不錯(cuò)的,謝謝了。
在經(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();
}
因?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();
}
您可能感興趣的文章:
相關(guān)文章
js 獲取中文拼音,Select自動(dòng)匹配字母獲取值的代碼
漢字轉(zhuǎn)拼音,拼音首字母,select選擇按聲母過濾2009-09-09JavaScript 輸入框內(nèi)容格式驗(yàn)證代碼
當(dāng)鼠標(biāo)焦點(diǎn)移至密碼輸入框時(shí),利用js腳本自動(dòng)驗(yàn)證用戶名的格式正確與否2010-02-02