Home > Article > Web Front-end > Tomato’s form validation code modified version_form effects
I posted it once on the classic forum. I use this thing in background processing in my personal projects. It is quite convenient for simple form verification.
Because I don’t want the code to become too bloated, there are many functions that are not commonly used and are not added.
There are no changes to the opinions of me from Foshan. Why? Because I'm lazy, haha
Today I saw that omeweb has also modified a version, made many changes, and the changes are quite good, thank you.
The source code is here:
//Remove spaces on both sides of the string
String.prototype.trim = function () {
return this.replace(/(^s ) |(s $)/g, "");
}
//Check whether the string is empty
String.prototype.isEmpty = function () {
return !(/.?[ ^s ] /.test(this));
}
//Check whether the value is between two specified values
String.prototype.isBetween = function (val, min, max) {
return isNaN(val) == false && val >= min && val }
//Get the maximum or minimum value
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('* ');
//String validation regular expression
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}-))?((0d{2,3})|0d{2,3} -)?[1-9]d{6,7}(-d
{1,4})?$/;
this.reg.mobile = /^(((d{2,3} ))|(d{3}-))?((13d{9})|(159d{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]$)/;
//Error output information
this.tip = new Object();
this.tip.unknow = 'Verification type not found, verification cannot be performed. ';
this.tip.paramError = 'The parameter setting is wrong and verification cannot be performed. ';
this.tip.required = 'Empty is not allowed. ';
this.tip.english = 'Only English characters and underscores (a-zA-Z0-9_) are allowed. ';
this.tip.chinese = 'Only Chinese characters allowed. ';
this.tip.number = 'is not a valid number. ';
this.tip.integer = 'is not a valid integer. ';
this.tip.float = 'Not a valid floating point number. ';
this.tip.date = 'Not a valid date format. (Example: 2007-06-29)';
this.tip.email = 'Not a valid email format. ';
this.tip.url = 'Not a valid hyperlink format.';
this.tip.phone = 'Not a valid phone number. ';
this.tip.mobile = 'Not a valid mobile phone number. ';
this.tip.ip = 'Not a valid IP address. ';
this.tip.zipcode = 'Not a valid zip code. ';
this.tip.qq = 'Not a valid QQ number. ';
this.tip.msn = 'Not a valid MSN account. ';
this.tip.idcard = 'Not a valid ID number.';
//Get the control name
this.getControlName = function ()
{
return this.element.getAttribute('controlName') == null
? 'Specify the value of the control '
ele. focus();
} catch (e){}
}
//Set border color
this.setBorderColor = function (ele) {
var borderColor = ele.currentStyle ?
ele.currentStyle.borderColor = '#ff9900';
ele.onkeyup = function () {
this.style.borderColor = borderColor;
}
}
//Output error feedback information
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); };
//Perform string validation
this.validate = function () {
var v = this.element.value;
//Verify whether non-empty is allowed
var required = this.element.getAttribute ('required');
if (required != null && v.isEmpty()) {
this.feedback('required');
return false;
}
//Verify whether the format is legal
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; ; }
//Perform data validation
var confirm = this.element.getAttribute('confirm');
if (confirm != null) {
try {
var data = eval( 'formObj.' confirm '.value');
');
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[i].tagName.toUpperCase() == 'SELECT' ||
this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
{
this.element = allTags[i];
if (!this.validate())
return false;
}
}
};
return this.init();
}