/*
* JavaScript Validation Framework
*
* Author: Michael Chen(mechiland) on 2004/03
* This software is on the http://www.cosoft.org.cn/projects/jsvalidation
* for update, bugfix, etc, you can goto the homepage and submit your request
* and question.
* Apache License 2.0
* You should use this software under the terms.
*
* Please, please keep above words. At least ,please make a note that such as
* "This software developed by Michael Chen(http://www.jzchen.net)" .
* $Id: validation-framework.js,v 1.7 2004/04/30 05:33:29 jzchen Exp $
*/
/** Config Section, Config these fields to make this framework work. **/
// If there is only one config file in your system, use this property. otherwise, use
// ValidationFramework.init("configfile")
// instead.
var ValidationRoot = "/ggcostc/js/";
// the field style when validation fails. it aim to provide more beautiful UI and more good
// experience to the end-user.
// NOTE: this will be buggy. Please report the error to me.
var ValidationFailCssStyle = "border:2px solid #FFCC88;";
//Validation function. The entry point of the framework.
function doValidate(formRef) {
try {
var formId = formRef;
if (typeof (formRef) == "string") {
formId = formRef;
} else if (typeof (formRef) == "object") {
formId = formRef.getAttribute("id");
}
var form = FormFactory.getFormFromId(formId);
if (form != null) {
return ValidationFramework.validateForm(form);
} else {
return false;
}
} catch (e) {
ValidationFramework.exception(e.name+":" +e.description);
return false;
}
}
/**===================================================================**/
/*
* JSValidation Framework Code Started
*
* Please do not modify the code unless you are very familiar with JavaScript.
* The best way to solve problem is report the problem to our project page.
* url: http://cosoft.org.cn/projects/jsvalidation
*/
// The Xml document. To process cross-browser. Thanks Eric.
function XmlDocument() {}
XmlDocument.create = function () {
if (document.implementation && document.implementation.createDocument) {
return document.implementation.createDocument("", "", null);
} else if (window.ActiveXObject) {
try {
var prefix = ["MSXML2", "MSXML", "Microsoft", "MSXML3"];
for (var i = 0; i < prefix.length; i++) {
//return new ActiveXObject(prefix[i] + ".DomDocument");
var obj = new ActiveXObject(prefix[i] + ".DomDocument");
if (obj == null || typeof(obj) == 'undefined') {
continue;
} else {
return obj;
}
}
} catch (e) {
//^_^
throw new Error("My God, What version of IE are you using? IE5&+ is requiered.");
}
} else
throw new Error("Cannot create DOM Document!");
}
function ValidationFramework() {}
ValidationFramework._validationCache = null;
ValidationFramework._currentForm = null;
ValidationFramework._userLanguage="auto";
/**
* Validate a form.
* NOTE: the form is Framework virture form, not the HTML Form.
* Html Form can be transform to Virture form by
* FormFactory.getFormFromId(htmlFormId);
* See the doc for more information.
* @param form the virtual form.
*/
ValidationFramework.validateForm = function(fform) {
ValidationFramework._currentForm = fform;
var failFields = [];
var id = fform.getId();
var showError = fform.getShowError();
var showType = fform.getShowType();
var br = null;
if (showError != "alert") {
br = "<br />";
} else {
br = "\n";
}
var errorStrArray = [];
var ret = false;
var formObj = document.getElementById(id);
var fields = fform.getFields();
var rightnum = 0;
for (var i = 0; i < fields.length; i++) {
var retVal = ValidationFramework.validateField(fields[i]);
var fo=formObj[fields[i].getName()];
if (typeof (fo) !='undefined' &&
fo != null &&
typeof(fo.type) != "undefined") {
fo.style.cssText = "";
}
if (retVal != "OK") {
errorStrArray[errorStrArray.length] = retVal;
failFields[failFields.length] = formObj[fields[i].getName()];
} else {
rightnum ++;
}
}
if (rightnum == fields.length) {
ret = true;
}
if (errorStrArray.length > 0) {
if (showError == "alert") {
if (showType == "first") {
alert(errorStrArray[0]);
} else {
alert(errorStrArray.join(br));
}
} else {
var errObj = document.getElementById(showError);
if (showType == "first") {
errObj.innerHTML = errorStrArray[0];
} else {
errObj.innerHTML = errorStrArray.join(br);
}
}
if (typeof (failFields[0]) !='undefined' &&
failFields[0] != null &&
typeof(failFields[0].type) != "undefined") {
failFields[0].focus();
}
for (var i = 0; i < failFields.length; i++) {
var o = failFields[i];
if ( typeof (o) !='undefined' &&
o != null && typeof(o.type) != "undefined") {
o.style.cssText = ValidationFailCssStyle;
}
}
}
return ret;
}
/**
* Validation the field
* @param filed the field you want to validate.
*/
ValidationFramework.validateField = function(field) {
var depends = field.getDepends();
var retStr = "OK";
for (var i = 0; i < depends.length; i++) {
if (!ValidationFramework.validateDepend(field, depends[i])) {
retStr = ValidationFramework.getErrorString(field, depends[i]);
return retStr; //Break;
}
}
return retStr;
}
/**
* Validate the field depend.
* This function dispatch the various depends into ValidateMethodFactory.validateXXX
*/
ValidationFramework.validateDepend = function(field, depend) {
if (depend.getName() == "required") {
return ValidateMethodFactory.validateRequired(field, depend.getParams());
} else if (depend.getName() == "integer") {
return ValidateMethodFactory.validateInteger(field, depend.getParams());
} else if (depend.getName() == "double") {
return ValidateMethodFactory.validateDouble(field, depend.getParams());
} else if (depend.getName() == "commonChar") {
return ValidateMethodFactory.validateCommonChar(field, depend.getParams());
} else if (depend.getName() == "chineseChar") {
return ValidateMethodFactory.validateChineseChar(field, depend.getParams());
} else if (depend.getName() == "minLength") {
return ValidateMethodFactory.validateMinLength(field, depend.getParams());
} else if (depend.getName() == "maxLength") {
return ValidateMethodFactory.validateMaxLength(field, depend.getParams());
} else if (depend.getName() == "email") {
return ValidateMethodFactory.validateEmail(field, depend.getParams());
} else if (depend.getName() == "date") {
return ValidateMethodFactory.validateDate(field, depend.getParams());
} else if (depend.getName() == "time") {
return ValidateMethodFactory.validateTime(field, depend.getParams());
} else if (depend.getName() == "mask") {
return ValidateMethodFactory.validateMask(field, depend.getParams());
} else if (depend.getName() == "integerRange") {
return ValidateMethodFactory.validateIntegerRange(field, depend.getParams());
} else if (depend.getName() == "doubleRange") {
return ValidateMethodFactory.validateDoubleRange(field, depend.getParams());
} else if (depend.getName() == "equalsField") {
return ValidateMethodFactory.validateEqualsField(field, depend.getParams());
} else {
ValidationFramework.exception("don't support�� " + depend.getName());
return false;
}
}
// hold the current config file
var _validationConfigFile = "";
ValidationFramework.getDocumentElement = function() {
if (ValidationFramework._validationCache != null) {
return ValidationFramework._validationCache;
}
var file = "";
if (_validationConfigFile != "") {
file = _validationConfigFile;
} else {
file = ValidationRoot + "validation-config.xml";
}
var xmlDoc = XmlDocument.create();
xmlDoc.async = false; // Damn!!! it cost me half an hour to fix it!
xmlDoc.lo
validation js表单验证
4星 · 超过85%的资源 需积分: 9 96 浏览量
2008-07-22
10:30:44
上传
评论
收藏 7KB RAR 举报
wgqsk2
- 粉丝: 0
- 资源: 10
最新资源
- 2013-2022vc运行命令库
- probleme.exe
- 基于python完成对csv文件的读取并且通过flask框架显示
- 基于SpringBoot的“体质测试数据分析及可视化”的设计与实现.zip
- c语言-c语言编程基础之leetcode题解第21题合并两个有序链表.zip
- 快速开发API服务的框架
- c语言-c语言编程基础之leetcode题解第20题有效的括号.zip
- c语言-c语言编程基础之leetcode题解第19题删除链表的倒数第N个结点.zip
- c语言-c语言编程基础之leetcode题解第17题电话号码的字母组合.zip
- c语言-c语言编程基础之leetcode题解第16题最接近的三数之和.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈