?? validation-framework.js
字號:
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 + -