⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validation-framework.js

📁 一个简单的BBS系统 适用于初学者用JSP和连接池 开发系统
💻 JS
📖 第 1 页 / 共 3 页
字号:
			lang = navigator.userLanguage.toLowerCase();
	}
	// get the language
	if (typeof ValidationErrorString[lang] != 'object') {
		stringResource = ValidationErrorString['zh-cn'];
	} else {
		stringResource = ValidationErrorString[lang];
	}
	var dep = depend.getName().toLowerCase();
	var retStr = stringResource[dep];
	//If the specified depend not defined, use the default error string.
	if (typeof retStr != 'string') {
		retStr = stringResource["default"];
		retStr = retStr.replace("{0}", field.getDisplayName());
		return retStr;
	}
	retStr = retStr.replace("{0}", field.getDisplayName());
	if (dep == "minlength" || dep == "maxlength" || dep == "date" ) {
		retStr = retStr.replace("{1}", depend.getParams()[0]);
	} else if ( dep == "equalsfield") {
		var eqField = field.getForm().findField(depend.getParams()[0]);
		if (eqField == null) {
			ValidationFramework.exception("找不到名称为[" + depend.getParams()[0]+"]的域,请检查xml配置文件。");
			retStr = "<<配置错误>>";
		} else {
			retStr = retStr.replace("{1}", field.getForm().findField(depend.getParams()[0]).getDisplayName());
		}
	} else if (dep == "integerrange" || dep == "doublerange") {
		retStr = retStr.replace("{1}", depend.getParams()[0]);
		retStr = retStr.replace("{2}", depend.getParams()[1]);
	}

	return retStr;
}

ValidationFramework.getWebFormFieldObj = function(field) {
	var obj = null;
	if (ValidationFramework._currentForm != null) {
		var formObj = document.getElementById(ValidationFramework._currentForm.getId());
		obj = formObj[field.getName()];
		if (typeof(obj) == 'undefined') {
			obj = null;
		}
	}
	if (obj == null) {
		ValidationFramework.exception("在配置文件中有需要验证的域,但在实际网页表单中不存在:[name=" + field.getName() + "]。");
	}
	return obj;
}

ValidationFramework.exception = function(str) {
	var ex = "JavaScript Validation Framework 运行时错误:\n\n";
	ex += str;
	ex += "\n\n\n任何运行错误都会导致该域验证失败。";
	alert(ex);
}
ValidationFramework.getIntegerValue = function(val) {
	var intvalue = parseInt(val);
	if (isNaN(intvalue)) {
		ValidationFramework.exception("期待一个整型参数。");
	}
	return intvalue;
}
ValidationFramework.getFloatValue = function(val) {
	var floatvalue = parseFloat(val);
	if (isNaN(floatvalue)) {
		ValidationFramework.exception("期待一个浮点型参数。");
	}
	return floatvalue;
}
/**
 * FormFactory
 * Build virture form from Html Form.
 */
function FormFactory() {}
FormFactory.getFormFromDOM = function(dom) {
	var form = new ValidationForm();
	form.setId(dom.getAttribute("id"));
	form.setShowError(dom.getAttribute("show-error"));
	form.setOnFail(dom.getAttribute("onfail"));
	form.setShowType(dom.getAttribute("show-type"));

	if (dom.hasChildNodes()) {
		var f = dom.childNodes;
		for (var i = 0; i < f.length; i++) {
			if (f.item(i) == null||typeof(f.item(i).tagName) == 'undefined' || f.item(i).tagName != 'field') {
				continue;
			}
			var field = FieldFactory.getFieldFromDOM(f.item(i));
			if (field != null) {
				form.addField(field);
			}
		}
	}
	return form;
}
/// Get the Form from ID
FormFactory.getFormFromId = function(id) {
	var root = ValidationFramework.getDocumentElement();
	if ( root == null || (!root.hasChildNodes()) ) return null;
	var vforms = root.childNodes;
	for (var i = 0; i < vforms.length; i++) {
		var f = vforms.item(i);
		if (typeof(f.tagName) != 'undefined' && f.tagName == 'form' && f.getAttribute("id") == id) {
			return FormFactory.getFormFromDOM(f);
		}
	}
	return null;
}

/**
 * A validation form object.
 */
function ValidationForm() {
	this._fields = [];
	this._id = null;
	this._showError = null;
	this._onFail = null;
	this._showType = null;

	this.getFields = function() { return this._fields; }
	this.setFields = function(p0) { this._fields = p0; }

	this.getId = function() { return this._id; }
	this.setId = function(p0) { this._id = p0; }

	this.getShowError = function() { return this._showError; }
	this.setShowError = function(p0) { this._showError = p0; }

	this.getShowType = function() { return this._showType; }
	this.setShowType = function(p0) { this._showType = p0; }

	this.getOnFail = function() { return this._onFail; }
	this.setOnFail = function(p0) { this._onFail = p0; }
	
	// find field by it's name
	this.findField = function(p0) {
		for (var i = 0; i < this._fields.length; i++) {
			if (this._fields[i].getName() == p0) {
				return this._fields[i];
			}
		}
		return null;
	}
	
	this.addField = function(p0) {
		this._fields[this._fields.length] = p0;
		p0.setForm(this);
	}
}

/**
 * A form filed. virtual.
 */
function ValidationField() {
	this._name = null;
	this._depends = [];
	this._displayName = null;
	this._onFail = null;
	this._form = null;

	this.getName = function() { return this._name; }
	this.setName = function(p0) { this._name = p0; }

	this.getDepends = function() { return this._depends; }
	this.setDepends = function(p0) { this._depends = p0; }

	this.getDisplayName = function() { return this._displayName; }
	this.setDisplayName = function(p0) { this._displayName = p0; }

	this.getOnFail = function() { return this._onFail; }
	this.setOnFail = function(p0) { this._onFail = p0; }
	
	this.getForm = function() { return this._form; }
	this.setForm = function(p0) { this._form = p0; }

	this.addDepend = function(p0) {
		this._depends[this._depends.length] = p0;
	}
}

///Factory methods for create Field
function FieldFactory() {}
FieldFactory.getFieldFromDOM = function(dom) {
	var field = new ValidationField();
	field.setName(dom.getAttribute("name"));
	field.setDisplayName(dom.getAttribute("display-name"));
	field.setOnFail(dom.getAttribute("onfail"));
	if (dom.hasChildNodes()) {
		var depends = dom.childNodes;
		for (var i = 0; i < depends.length; i++) {
			var item = depends.item(i);
			if (typeof(item.tagName) == 'undefined' || item.tagName != 'depend') {
				continue;
			}
			var dp = new ValidationDepend();
			dp.setName(item.getAttribute("name"));
			dp.addParam(item.getAttribute("param0"));
			dp.addParam(item.getAttribute("param1"));
			dp.addParam(item.getAttribute("param2"));
			dp.addParam(item.getAttribute("param3"));
			dp.addParam(item.getAttribute("param4"));
			field.addDepend(dp);
		}
	}
	return field;
}


function FormFieldUtils() {}

FormFieldUtils.findField = function(formName, fieldName) {
	
	var formArr = ValidationFramework.getAllForms();
	var theForm = null;
	for (var i = 0; i < formArr.length; i++) {
		if (formArr[i].getName() == formName) {
			theForm = formArr[i];
		}
	}

	if (theForm != null) {
		return theForm.findField(fieldName);
	} else {
		return null;
	}
}

/**
 * A validaton depend.
 */
function ValidationDepend() {
	this._name = null;
	this._params = [];

	this.getName = function() { return this._name; }
	this.setName = function(p0) { this._name = p0; }

	this.getParams = function() { return this._params; }
	this.setParams = function(p0) { this.params = p0; }

	this.addParam = function(p0) {
		this._params[this._params.length] = p0;
	}
}

function ValidateMethodFactory() {}
ValidateMethodFactory._methods = [];
ValidateMethodFactory.validateRequired = function(field, params) {
	var obj = ValidationFramework.getWebFormFieldObj(field);	
	if (obj == null) return false;
	if (typeof(obj.type) == "undefined") {
		var tmp = 0;
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked) {
				return true;
			}
		}
		return false;
	}

	if (obj.type == "checkbox" || obj.type == "radio") {
		return (obj.checked);
	} else {
		return !(obj.value == "");
	}
}

ValidateMethodFactory.validateInteger = function(field, params) {
	var obj = ValidationFramework.getWebFormFieldObj(field);
	if (obj == null) return false;
	if (obj.value == "") return true;
	var exp = new RegExp("^-?\\d+$");
	return exp.test(obj.value);
}

ValidateMethodFactory.validateDouble = function(field, params) {
	var obj = ValidationFramework.getWebFormFieldObj(field);
	if (obj == null) return false;
	if (obj.value == "") return true;
	var exp = new RegExp("^-?\\d+\.\\d+$");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -