/*
* 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 $
* update :
1) 2007-12-16 add support of firefox 2. // by lein
Main changes:
a. In ValidationFramework.getDocumentElement, a little difference of 'xmlDoc' between firefox and ie was incarnated.
b. In FormFactory.getFormFromId, firefox doesn't support has item property of xmldoc.
*/
/** 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.
//alert(window.location.href)
var ValidationRoot = "";
// 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.
// The style has been changed at 2007-12-10;
var ValidationFailCssStyle = "border:1px solid #F77B00; color:#D90005";
//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");
}
//alert(formId)
var form = FormFactory.getFormFromId(formId);
if (form != null) {
//alert(form)
return ValidationFramework.validateForm(form);
} else {
return false;
}
} catch (e) {
ValidationFramework.exception(e.name+":" +e.description);
return false;
}
}
/**===================================================================**/
/* 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");
* 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 () {
//alert("XmlDocument.create ")
return Try.these(
function(){return new ActiveXObject('MSXML2.DomDocument')},
function(){return new ActiveXObject('MSXML.DomDocument')},
function(){return new ActiveXObject('Microsoft.DomDocument')},
function(){return new ActiveXObject('MSXML3.DomDocument')}
) || false;
};
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();
//alert(id+"|"+showError+"|"+showType+"110")
var br = null;
if (showError != "alert") {
br = "<br />";
} else {
br = " ";
}
var errorStrArray = [];
var ret = false;
var formObj = document.getElementById(id);
////alert("formObj.tagName is "+formObj.getTagName())
formObj=document.getElementById(FormToChk);
//alert("ok")
var fields = fform.getFields();
//alert("fields.length="+fields.length)
var rightnum = 0;
for (var i = 0; i < fields.length; i++) {
//alert(fields[i])
var retVal = ValidationFramework.validateField(fields[i]);
var fo=formObj[fields[i].getName()];
//alert(fo+"|fields[i].getName()="+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 if(showError == "errorDiv") {
AddtionDOMName=AddtionErrorDOMName || ""
showError=showError+AddtionDOMName
for(var i=0;i<failFields.length;i++){
var errObj = document.getElementById(showError+failFields[i].name);
errObj.innerHTML = errorStrArray[i];
}
}else{
AddtionDOMName=AddtionErrorDOMName || ""
showError=showError+AddtionDOMName
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";
//alert("depends.length="+depends.length)
for (var i = 0; i < depends.length; i++) {
//alert(field+"|"+depends[i]+"<------")
if (!ValidationFramework.validateDepend(field, depends[i])) {
//alert("There foudn an error")
retStr = ValidationFramework.getErrorString(field, depends[i]);
return retStr; //Break;
}else{
//alert("Rule "+i+" passed")
}
}
return retStr;
}
/**
* Validate the field depend.
* This function dispatch the various depends into ValidateMethodFactory.validateXXX
*/
ValidationFramework.validateDepend = function(field, depend) {
//alert("depend.getName()="+depend.getName())
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 if (depend.getName() == "numberrange") {
return ValidateMethodFactory.validateNumberRange(field, depend.getParams());
} else {
ValidationFramework.exception("Hasn't implemented that depend: " + depend.getName());
return false;
}
}
// hold the current config file
var _validationConfigFile = "";
ValidationFramework.getDocumentElement = function() {
if (ValidationFramework._validationCache != null) {
return ValidationFramework._validationCache;
}
//path=window.location.pathname.substr(0,window.location.pathname.lastIndexOf("/"))
var file = "";
if (_validationConfigFile != "") {
if(window.location.protocol=="http:"){
file = window.location.protocol+"//"+window.location.host+"/"+_validationConfigFile;
}else{
file =_validationConfigFile;
}
} else {
file = ValidationRoot + "validation-config.xml";
}
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.setProperty("ServerHTTPRequest",true);
//xmlDoc.setRequestHeader("If-Modified-Since","0");
xmlDoc.load(file);
if (xmlDoc.parseError.errorCode!=0)
{
var error = xmlDoc.parseError;
ValidationFramework.exception(error.reason)
return false;
}
if (xmlDoc.documentElement == null) {
ValidationFramework.exception("Configure Error, please check!");
return null;
}
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.async = false; // Damn!!! it cost me half an hour to fix it!
xmlDoc.load(file);
if(xmlDoc.documentElement.nodeName=='parsererror'){
ValidationFramework.exception(xmlDoc.documentElement.textContent)
return null;
}
}
else
{
XMLMSG='Sorry, your browser doesn't support XML.'
alert(XMLMSG);
return false;
}
// TODO: parse the document if it's a valid document.
ValidationFramework._validationCache = xmlDoc.documentElement;
//alert("xmlDoc.documentElement:"+xmlDoc.documentElement+"|"+typeof(xmlDoc.documentElement))
//alert("ValidationFramework._validationCache:"+ValidationFramework._validationCache +typeof(ValidationFramework._validationCache))
var lang=ValidationFramework._validationCache.getAttribute("lang");
//alert(lang)
ValidationFramework._userLanguage = (lang==null) ? "auto" : lang;
return ValidationFramework._validationCache;
}
ValidationFramework.init = function(configFile) {
_validationConfigFile = configFile;
var configdocument=ValidationFramework.getDocumentElement();
//alert("configdocument="+configdocument+"|"+typeof(configdocument))
}
ValidationFramework.getAllForms = function() {
var vforms = [];
var root = ValidationFramework.getDocumentElement();
if (root != null) {
var fs = root.childNodes;
for (var i = 0;i < fs.length ;i++ ) {
vforms[i] = new ValidationForm(fs.item(i));
}
}
return vforms;
}
ValidationFramework.getErrorString = function(field, depend) {
var stringResource = null;
var lang = ValidationFramework._userLanguage.toLowerCase();
//if lang == auto, get the user's browser language.
if (lang == "auto") {
// different browser has the different method the get the
// user's language. so this is a stupid way to detect the
// most common browser IE and Mozilla.
if (typeof navigator.userLanguage == 'undefined')
lang = navigator.language.toLowerCase();
else
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"];/////////////////////////////////////////////////////
disp_field_str=ValidationErrorString[field.getDisplayName()]||field.getDisplayName()
retStr = retStr.replace("{0}", disp_field_str);
return retStr;
}
disp_field_str=ValidationErrorString[field.getDisplayName()]||field.getDisplayName()
retStr = retStr.replace("{0}", disp_field_str);
if (dep == "minlength" || dep == "maxlength" || dep == "date" ) {
disp_field_str=ValidationErrorString[depend.getParams()[0]]||depend.getParams()[0]
retStr = retStr.replace("{1}", depend.getParams()[0]);
} else if ( dep == "equalsfield") {
var eqField = field.getForm().findField(depend.getParams()[0]);
if (eqField == null) {
ValidationFramework.exception("Cann't find the filed which name is [" + depend.getParams()[0]+"], please check the xml configure file.");
retStr = "<<Conifgure Error>>";
} else {
disp_field_str=ValidationErrorString[field.getForm().findField(depend.getParams()[0]).getDisplayName()]||field.getForm().findField(depend.getParams()[0]).getDisplayName()
retStr = retStr.replace("{1}", disp_field_str);
}
} else if (dep == "integerrange" || dep == "doublerange" || dep == "numberrange") {
disp_field_str=ValidationErrorString[depend.getParams()[0]]||depend.getParams()[0]
retStr = retStr.replace("{1}", disp_field_str);
disp_field_str=ValidationErrorString[depend.getParams()[1]]||depend.getParams()[1]
retStr = retStr.replace("{2}", disp_field_str);
}
return retStr;
}
ValidationFramework.getWebFormFieldObj = function(field) {
var obj = null;
if (ValidationFramework._currentForm != null) {
var formObj = document.getElementById(ValidationFramework._currentForm.getId());
try{
obj = formObj[field.getName()];
}catch(e){
formObj = document.getElementById(FormToChk);
obj = formObj[field.getName()];
}
if (typeof(obj) == 'undefined') {
obj = null;
}
}else{
//alert("ValidationFramework._currentForm === null")
}
if (obj == null) {
ValidationFramework.exception("Can not find form filed which name is [" + field.getName() + ".");
}
return obj;
}
ValidationFramework.exception = function(str) {
var ex = "JavaScript Validation Framework runtime error: ";
ex += str;
ex += " Any runtime error will cause the validation failure.";
alert(ex);
}
ValidationFramework.getIntegerValue = function(val) {
var intvalue = parseInt(val);
if (isNaN(intvalue)) {
ValidationFramework.exception("Expect an integer.");
}
return intvalue;
}
ValidationFramework.getFloatValue = function(val) {
var floatvalue = parseFloat(val);
if (isNaN(floatvalue)) {
ValidationFramework.exception("Expect a float number.");
}
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;
if(vforms.length<2){//如果配置xml文档只有一个配置表单
var f = document.all?vforms.item(0):vforms[i];
return FormFactory.getFormFromDOM(f);
}else{
if(!document.all){//Firefox will give three elements though there's only one form.
var tmp=[];
for (var i = 0; i < vforms.length; i++) {//The one which's tagname is not empty is we need
var f = vforms.item(i);
if(typeof(f.tagName) != 'undefined' &&f.tagName == 'form'){
tmp[tmp.length]=f;
}
}
vforms= tmp;
if(vforms.length<2){
var f = vforms[0];
return FormFactory.getFormFromDOM(f);
}
}
for (var i = 0; i < vforms.length; i++) {
var f = document.all?vforms.item(0):vforms[i];
if (typeof(f.tagName) != 'undefined' && f.tagName == 'form' && f.getAttribute("id") == id) {
return FormFactory.getFormFromDOM(f);
}else{
//alert(f.getAttribute("id") +"|"+id+"|"+f.tagName)
}
}
}
alert('The setting for the form('+id+') to be checked was not found!');
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+$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateCommonChar = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp("^[A-Za-z0-9_]*$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateChineseChar = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp("^[一-龥豈-鶴]*$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateMinLength = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var v = ValidationFramework.getIntegerValue(params[0]);
return (obj.value.length >= v);
}
ValidateMethodFactory.validateMaxLength = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var v = ValidationFramework.getIntegerValue(params[0]);
return (obj.value.length <= v);
}
ValidateMethodFactory.validateEmail = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
return ValidateMethodFactory.__checkEmail(obj.value);
}
ValidateMethodFactory.validateDate = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var value = obj.value;
var datePattern = params[0];
var MONTH = "mm";
var DAY = "dd";
var YEAR = "yyyy";
var orderMonth = datePattern.indexOf(MONTH);
var orderDay = datePattern.indexOf(DAY);
var orderYear = datePattern.indexOf(YEAR);
var bValid = true;
var dateRegexp = null;
if ((orderDay < orderYear && orderDay > orderMonth)) {
var iDelim1 = orderMonth + MONTH.length;
var iDelim2 = orderDay + DAY.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderDay && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})(/d{2})(/d{4})$");
} else if (iDelim1 == orderDay) {
dateRegexp = new RegExp("^(/d{2})(/d{2})[" + delim2 + "](/d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})(/d{4})$");
} else {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})[" + delim2 + "](/d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[2], matched[1], matched[3])) {
bValid = false;
}
} else {
bValid = false;
}
} else if ((orderMonth < orderYear && orderMonth > orderDay)) {
var iDelim1 = orderDay + DAY.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})(/d{2})(/d{4})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(/d{2})(/d{2})[" + delim2 + "](/d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})(/d{4})$");
} else {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})[" + delim2 + "](/d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[1], matched[2], matched[3])) {
bValid = false;
}
} else {
bValid = false;
}
} else if ((orderMonth > orderYear && orderMonth < orderDay)) {
var iDelim1 = orderYear + YEAR.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderDay) {
dateRegexp = new RegExp("^(/d{4})(/d{2})(/d{2})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(/d{4})(/d{2})[" + delim2 + "](/d{2})$");
} else if (iDelim2 == orderDay) {
dateRegexp = new RegExp("^(/d{4})[" + delim1 + "](/d{2})(/d{2})$");
} else {
dateRegexp = new RegExp("^(/d{4})[" + delim1 + "](/d{2})[" + delim2 + "](/d{2})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[3], matched[2], matched[1])) {
bValid = false;
}
} else {
bValid = false;
}
} else {
bValid = false;
}
return bValid;
}
ValidateMethodFactory.validateTime = function(field, params) {
////NOT IMPLEMENT YET SINCE IT'S NOT USEFUL.
return true;
}
ValidateMethodFactory.validateMask = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp(params[0]);
//FIXME: this method may be buggy, need more test.
return exp.test(obj.value);
}
ValidateMethodFactory.validateIntegerRange = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getIntegerValue(params[0]);
var p1 = ValidationFramework.getIntegerValue(params[1]);
if (ValidateMethodFactory.validateInteger(field)) {
var v = parseInt(obj.value);
return (v >= p0 && v <= p1);
} else {
return false;
}
return true;
}
ValidateMethodFactory.validateDoubleRange = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getFloatValue(params[0]);
var p1 = ValidationFramework.getFloatValue(params[1]);
if (ValidateMethodFactory.validateInteger(field) || ValidateMethodFactory.validateDouble(field)) {
var v = parseFloat(obj.value);
return (v >= p0 && v <= p1);
} else {
return false;
}
return true;
}
ValidateMethodFactory.validateEqualsField = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var formObj = document.getElementById(ValidationFramework._currentForm.getId());
try{
var eqField = formObj[params[0]];
}catch(e){
formObj = document.getElementById(FormToChk);
var eqField = formObj[params[0]];
}
if (eqField != null) {
return (obj.value == eqField.value)
} else {
return false;
}
}
ValidateMethodFactory.validateNumberRange = function(field, params) {//by lein
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getIntegerValue(params[0]);
var p1 = ValidationFramework.getIntegerValue(params[1]);
var v = parseFloat(obj.value);
if(isNaN(v)||v.length<1){
return false
}
return (v >= p0 && v <= p1);
}
ValidateMethodFactory.__isValidDate = function(day, month, year) {
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
if ((month == 4 || month == 6 || month == 9 || month == 11) &&(day == 31))
return false;
if (month == 2) {
var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day == 29 && !leap)) return false;
}
return true;
}
/**
* Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
* http://javascript.internet.com
*/
ValidateMethodFactory.__checkEmail = function(emailStr) {
if (emailStr.length == 0) {
return true;
}
var emailPat=/^(.+)@(.+)$/;
var specialChars="/(/)<>@,;:///"/./[/]";
var validChars="[^/s" + specialChars + "]";
var quotedUser="("[^"]*")";
var ipDomainPat=/^(d{1,3})[.](d{1,3})[.](d{1,3})[.](d{1,3})$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(/." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(/." + atom + ")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray[i] > 255) {
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1].length < 2) ||
(domArr[domArr.length-1].length > 3)) {
return false;
}
if (len < 2) {
return false;
}
return true;
}
////Language Definitions
var ValidationErrorString = new Object();
////Simplified Chinese(zh-ch)
ValidationErrorString["zh-cn"] = new Object();
ValidationErrorString["zh-cn"]["default"]="域{0}校验失败。";
ValidationErrorString["zh-cn"]["required"]="{0}不能为空。";
ValidationErrorString["zh-cn"]["integer"]="{0}必须是一个整数。";
ValidationErrorString["zh-cn"]["double"]="{0}必须是一个浮点数(带小数点)。";
ValidationErrorString["zh-cn"]["commonchar"] = "{0}必须是普通英文字符:字母,数字和下划线。";
ValidationErrorString["zh-cn"]["chinesechar"] = "{0}必须是中文字符。";
ValidationErrorString["zh-cn"]["minlength"]="{0}长度不能小于{1}个字符。";
ValidationErrorString["zh-cn"]["maxlength"]="{0}长度不能大于{1}个字符。" ;
ValidationErrorString["zh-cn"]["invalid"]="{0}无效。";
ValidationErrorString["zh-cn"]["date"]="{0}不是一个有效日期,期待格式:{1}。";
ValidationErrorString["zh-cn"]["integerrange"]="{0}必须在整数{1}和{2}之间。";
ValidationErrorString["zh-cn"]["doublerange"]="{0}必须在浮点数{1}和{2}之间。";
ValidationErrorString["zh-cn"]["pid"]="{0}不是一个有效身份证号。";
ValidationErrorString["zh-cn"]["email"]="{0}不是一个有效的Email。";
ValidationErrorString["zh-cn"]["equalsfield"]="{0}必须和{1}一致。";
ValidationErrorString["zh-cn"]["numberrange"]="{0}必须在数值{1}和{2}之间。";
////English(en-us)
ValidationErrorString["en-us"] = new Object();
ValidationErrorString["en-us"]["default"]="Failed when validating filed {0}.";
ValidationErrorString["en-us"]["required"]="{0} is required.";
ValidationErrorString["en-us"]["integer"]="{0} must be a integer.";
ValidationErrorString["en-us"]["double"]="{0} must be a double value. ";
ValidationErrorString["en-us"]["commonchar"] = "{0} should be common ascii characters, A-Z,a-z and undercore. ";
ValidationErrorString["en-us"]["chinesechar"] = "{0} must be chinese characters. ";
ValidationErrorString["en-us"]["minlength"]="{0} cannot be less then {1}. ";
ValidationErrorString["en-us"]["maxlength"]="{0} cannot be more then {1}. ";
ValidationErrorString["en-us"]["invalid"]="{0} in invalid. ";
ValidationErrorString["en-us"]["date"]="{0} is not an invalid date format: {1}. ";
ValidationErrorString["en-us"]["integerrange"]="{0} should be between number {1} and {2}. ";
ValidationErrorString["en-us"]["doublerange"]="{0} should be between double {1} and {2}. ";
ValidationErrorString["en-us"]["pid"]="{0} is not an valid pid. ";
ValidationErrorString["en-us"]["email"]="{0} is not an valid email address. ";
ValidationErrorString["en-us"]["equalsfield"]="{0} should be agree with field {1}. ";
ValidationErrorString["en-us"]["numberrange"]="{0} should be between number {1} and {2}. ";
ValidationErrorString["zh-big5"] = new Object();
ValidationErrorString["zh-big5"]["default"]="域{0}校驗失敗。";
ValidationErrorString["zh-big5"]["required"]="{0}不能為空。";
ValidationErrorString["zh-big5"]["integer"]="{0}必須是一個整數。";
ValidationErrorString["zh-big5"]["double"]="{0}必須是一個浮點數(帶小數點)。";
ValidationErrorString["zh-big5"]["commonchar"] = "{0}必須是普通英文字元:字母,數位和下劃線。";
ValidationErrorString["zh-big5"]["chinesechar"] = "{0}必須是中文字元。";
ValidationErrorString["zh-big5"]["minlength"]="{0}長度不能小於{1}個字元。";
ValidationErrorString["zh-big5"]["maxlength"]="{0}長度不能大於{1}個字元。" ;
ValidationErrorString["zh-big5"]["invalid"]="{0}無效。";
ValidationErrorString["zh-big5"]["date"]="{0}不是一個有效日期,期待格式:{1}。";
ValidationErrorString["zh-big5"]["integerrange"]="{0}必須在整數{1}和{2}之間。";
ValidationErrorString["zh-big5"]["doublerange"]="{0}必須在浮點數{1}和{2}之間。";
ValidationErrorString["zh-big5"]["pid"]="{0}不是一個有效身份證號。";
ValidationErrorString["zh-big5"]["email"]="{0}不是一個有效的Email。";
ValidationErrorString["zh-big5"]["equalsfield"]="{0}必須和{1}一致。";
ValidationErrorString["zh-big5"]["numberrange"]="{0}必須在數值{1}和{2}之間。";
// preload the validation file.
//ValidationFramework.getDocumentElement();
* 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 $
* update :
1) 2007-12-16 add support of firefox 2. // by lein
Main changes:
a. In ValidationFramework.getDocumentElement, a little difference of 'xmlDoc' between firefox and ie was incarnated.
b. In FormFactory.getFormFromId, firefox doesn't support has item property of xmldoc.
*/
/** 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.
//alert(window.location.href)
var ValidationRoot = "";
// 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.
// The style has been changed at 2007-12-10;
var ValidationFailCssStyle = "border:1px solid #F77B00; color:#D90005";
//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");
}
//alert(formId)
var form = FormFactory.getFormFromId(formId);
if (form != null) {
//alert(form)
return ValidationFramework.validateForm(form);
} else {
return false;
}
} catch (e) {
ValidationFramework.exception(e.name+":" +e.description);
return false;
}
}
/**===================================================================**/
/* 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");
* 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 () {
//alert("XmlDocument.create ")
return Try.these(
function(){return new ActiveXObject('MSXML2.DomDocument')},
function(){return new ActiveXObject('MSXML.DomDocument')},
function(){return new ActiveXObject('Microsoft.DomDocument')},
function(){return new ActiveXObject('MSXML3.DomDocument')}
) || false;
};
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();
//alert(id+"|"+showError+"|"+showType+"110")
var br = null;
if (showError != "alert") {
br = "<br />";
} else {
br = " ";
}
var errorStrArray = [];
var ret = false;
var formObj = document.getElementById(id);
////alert("formObj.tagName is "+formObj.getTagName())
formObj=document.getElementById(FormToChk);
//alert("ok")
var fields = fform.getFields();
//alert("fields.length="+fields.length)
var rightnum = 0;
for (var i = 0; i < fields.length; i++) {
//alert(fields[i])
var retVal = ValidationFramework.validateField(fields[i]);
var fo=formObj[fields[i].getName()];
//alert(fo+"|fields[i].getName()="+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 if(showError == "errorDiv") {
AddtionDOMName=AddtionErrorDOMName || ""
showError=showError+AddtionDOMName
for(var i=0;i<failFields.length;i++){
var errObj = document.getElementById(showError+failFields[i].name);
errObj.innerHTML = errorStrArray[i];
}
}else{
AddtionDOMName=AddtionErrorDOMName || ""
showError=showError+AddtionDOMName
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";
//alert("depends.length="+depends.length)
for (var i = 0; i < depends.length; i++) {
//alert(field+"|"+depends[i]+"<------")
if (!ValidationFramework.validateDepend(field, depends[i])) {
//alert("There foudn an error")
retStr = ValidationFramework.getErrorString(field, depends[i]);
return retStr; //Break;
}else{
//alert("Rule "+i+" passed")
}
}
return retStr;
}
/**
* Validate the field depend.
* This function dispatch the various depends into ValidateMethodFactory.validateXXX
*/
ValidationFramework.validateDepend = function(field, depend) {
//alert("depend.getName()="+depend.getName())
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 if (depend.getName() == "numberrange") {
return ValidateMethodFactory.validateNumberRange(field, depend.getParams());
} else {
ValidationFramework.exception("Hasn't implemented that depend: " + depend.getName());
return false;
}
}
// hold the current config file
var _validationConfigFile = "";
ValidationFramework.getDocumentElement = function() {
if (ValidationFramework._validationCache != null) {
return ValidationFramework._validationCache;
}
//path=window.location.pathname.substr(0,window.location.pathname.lastIndexOf("/"))
var file = "";
if (_validationConfigFile != "") {
if(window.location.protocol=="http:"){
file = window.location.protocol+"//"+window.location.host+"/"+_validationConfigFile;
}else{
file =_validationConfigFile;
}
} else {
file = ValidationRoot + "validation-config.xml";
}
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.setProperty("ServerHTTPRequest",true);
//xmlDoc.setRequestHeader("If-Modified-Since","0");
xmlDoc.load(file);
if (xmlDoc.parseError.errorCode!=0)
{
var error = xmlDoc.parseError;
ValidationFramework.exception(error.reason)
return false;
}
if (xmlDoc.documentElement == null) {
ValidationFramework.exception("Configure Error, please check!");
return null;
}
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.async = false; // Damn!!! it cost me half an hour to fix it!
xmlDoc.load(file);
if(xmlDoc.documentElement.nodeName=='parsererror'){
ValidationFramework.exception(xmlDoc.documentElement.textContent)
return null;
}
}
else
{
XMLMSG='Sorry, your browser doesn't support XML.'
alert(XMLMSG);
return false;
}
// TODO: parse the document if it's a valid document.
ValidationFramework._validationCache = xmlDoc.documentElement;
//alert("xmlDoc.documentElement:"+xmlDoc.documentElement+"|"+typeof(xmlDoc.documentElement))
//alert("ValidationFramework._validationCache:"+ValidationFramework._validationCache +typeof(ValidationFramework._validationCache))
var lang=ValidationFramework._validationCache.getAttribute("lang");
//alert(lang)
ValidationFramework._userLanguage = (lang==null) ? "auto" : lang;
return ValidationFramework._validationCache;
}
ValidationFramework.init = function(configFile) {
_validationConfigFile = configFile;
var configdocument=ValidationFramework.getDocumentElement();
//alert("configdocument="+configdocument+"|"+typeof(configdocument))
}
ValidationFramework.getAllForms = function() {
var vforms = [];
var root = ValidationFramework.getDocumentElement();
if (root != null) {
var fs = root.childNodes;
for (var i = 0;i < fs.length ;i++ ) {
vforms[i] = new ValidationForm(fs.item(i));
}
}
return vforms;
}
ValidationFramework.getErrorString = function(field, depend) {
var stringResource = null;
var lang = ValidationFramework._userLanguage.toLowerCase();
//if lang == auto, get the user's browser language.
if (lang == "auto") {
// different browser has the different method the get the
// user's language. so this is a stupid way to detect the
// most common browser IE and Mozilla.
if (typeof navigator.userLanguage == 'undefined')
lang = navigator.language.toLowerCase();
else
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"];/////////////////////////////////////////////////////
disp_field_str=ValidationErrorString[field.getDisplayName()]||field.getDisplayName()
retStr = retStr.replace("{0}", disp_field_str);
return retStr;
}
disp_field_str=ValidationErrorString[field.getDisplayName()]||field.getDisplayName()
retStr = retStr.replace("{0}", disp_field_str);
if (dep == "minlength" || dep == "maxlength" || dep == "date" ) {
disp_field_str=ValidationErrorString[depend.getParams()[0]]||depend.getParams()[0]
retStr = retStr.replace("{1}", depend.getParams()[0]);
} else if ( dep == "equalsfield") {
var eqField = field.getForm().findField(depend.getParams()[0]);
if (eqField == null) {
ValidationFramework.exception("Cann't find the filed which name is [" + depend.getParams()[0]+"], please check the xml configure file.");
retStr = "<<Conifgure Error>>";
} else {
disp_field_str=ValidationErrorString[field.getForm().findField(depend.getParams()[0]).getDisplayName()]||field.getForm().findField(depend.getParams()[0]).getDisplayName()
retStr = retStr.replace("{1}", disp_field_str);
}
} else if (dep == "integerrange" || dep == "doublerange" || dep == "numberrange") {
disp_field_str=ValidationErrorString[depend.getParams()[0]]||depend.getParams()[0]
retStr = retStr.replace("{1}", disp_field_str);
disp_field_str=ValidationErrorString[depend.getParams()[1]]||depend.getParams()[1]
retStr = retStr.replace("{2}", disp_field_str);
}
return retStr;
}
ValidationFramework.getWebFormFieldObj = function(field) {
var obj = null;
if (ValidationFramework._currentForm != null) {
var formObj = document.getElementById(ValidationFramework._currentForm.getId());
try{
obj = formObj[field.getName()];
}catch(e){
formObj = document.getElementById(FormToChk);
obj = formObj[field.getName()];
}
if (typeof(obj) == 'undefined') {
obj = null;
}
}else{
//alert("ValidationFramework._currentForm === null")
}
if (obj == null) {
ValidationFramework.exception("Can not find form filed which name is [" + field.getName() + ".");
}
return obj;
}
ValidationFramework.exception = function(str) {
var ex = "JavaScript Validation Framework runtime error: ";
ex += str;
ex += " Any runtime error will cause the validation failure.";
alert(ex);
}
ValidationFramework.getIntegerValue = function(val) {
var intvalue = parseInt(val);
if (isNaN(intvalue)) {
ValidationFramework.exception("Expect an integer.");
}
return intvalue;
}
ValidationFramework.getFloatValue = function(val) {
var floatvalue = parseFloat(val);
if (isNaN(floatvalue)) {
ValidationFramework.exception("Expect a float number.");
}
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;
if(vforms.length<2){//如果配置xml文档只有一个配置表单
var f = document.all?vforms.item(0):vforms[i];
return FormFactory.getFormFromDOM(f);
}else{
if(!document.all){//Firefox will give three elements though there's only one form.
var tmp=[];
for (var i = 0; i < vforms.length; i++) {//The one which's tagname is not empty is we need
var f = vforms.item(i);
if(typeof(f.tagName) != 'undefined' &&f.tagName == 'form'){
tmp[tmp.length]=f;
}
}
vforms= tmp;
if(vforms.length<2){
var f = vforms[0];
return FormFactory.getFormFromDOM(f);
}
}
for (var i = 0; i < vforms.length; i++) {
var f = document.all?vforms.item(0):vforms[i];
if (typeof(f.tagName) != 'undefined' && f.tagName == 'form' && f.getAttribute("id") == id) {
return FormFactory.getFormFromDOM(f);
}else{
//alert(f.getAttribute("id") +"|"+id+"|"+f.tagName)
}
}
}
alert('The setting for the form('+id+') to be checked was not found!');
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+$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateCommonChar = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp("^[A-Za-z0-9_]*$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateChineseChar = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp("^[一-龥豈-鶴]*$");
return exp.test(obj.value);
}
ValidateMethodFactory.validateMinLength = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var v = ValidationFramework.getIntegerValue(params[0]);
return (obj.value.length >= v);
}
ValidateMethodFactory.validateMaxLength = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var v = ValidationFramework.getIntegerValue(params[0]);
return (obj.value.length <= v);
}
ValidateMethodFactory.validateEmail = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
return ValidateMethodFactory.__checkEmail(obj.value);
}
ValidateMethodFactory.validateDate = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var value = obj.value;
var datePattern = params[0];
var MONTH = "mm";
var DAY = "dd";
var YEAR = "yyyy";
var orderMonth = datePattern.indexOf(MONTH);
var orderDay = datePattern.indexOf(DAY);
var orderYear = datePattern.indexOf(YEAR);
var bValid = true;
var dateRegexp = null;
if ((orderDay < orderYear && orderDay > orderMonth)) {
var iDelim1 = orderMonth + MONTH.length;
var iDelim2 = orderDay + DAY.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderDay && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})(/d{2})(/d{4})$");
} else if (iDelim1 == orderDay) {
dateRegexp = new RegExp("^(/d{2})(/d{2})[" + delim2 + "](/d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})(/d{4})$");
} else {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})[" + delim2 + "](/d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[2], matched[1], matched[3])) {
bValid = false;
}
} else {
bValid = false;
}
} else if ((orderMonth < orderYear && orderMonth > orderDay)) {
var iDelim1 = orderDay + DAY.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})(/d{2})(/d{4})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(/d{2})(/d{2})[" + delim2 + "](/d{4})$");
} else if (iDelim2 == orderYear) {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})(/d{4})$");
} else {
dateRegexp = new RegExp("^(/d{2})[" + delim1 + "](/d{2})[" + delim2 + "](/d{4})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[1], matched[2], matched[3])) {
bValid = false;
}
} else {
bValid = false;
}
} else if ((orderMonth > orderYear && orderMonth < orderDay)) {
var iDelim1 = orderYear + YEAR.length;
var iDelim2 = orderMonth + MONTH.length;
var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
if (iDelim1 == orderMonth && iDelim2 == orderDay) {
dateRegexp = new RegExp("^(/d{4})(/d{2})(/d{2})$");
} else if (iDelim1 == orderMonth) {
dateRegexp = new RegExp("^(/d{4})(/d{2})[" + delim2 + "](/d{2})$");
} else if (iDelim2 == orderDay) {
dateRegexp = new RegExp("^(/d{4})[" + delim1 + "](/d{2})(/d{2})$");
} else {
dateRegexp = new RegExp("^(/d{4})[" + delim1 + "](/d{2})[" + delim2 + "](/d{2})$");
}
var matched = dateRegexp.exec(value);
if(matched != null) {
if (!ValidateMethodFactory.__isValidDate(matched[3], matched[2], matched[1])) {
bValid = false;
}
} else {
bValid = false;
}
} else {
bValid = false;
}
return bValid;
}
ValidateMethodFactory.validateTime = function(field, params) {
////NOT IMPLEMENT YET SINCE IT'S NOT USEFUL.
return true;
}
ValidateMethodFactory.validateMask = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var exp = new RegExp(params[0]);
//FIXME: this method may be buggy, need more test.
return exp.test(obj.value);
}
ValidateMethodFactory.validateIntegerRange = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getIntegerValue(params[0]);
var p1 = ValidationFramework.getIntegerValue(params[1]);
if (ValidateMethodFactory.validateInteger(field)) {
var v = parseInt(obj.value);
return (v >= p0 && v <= p1);
} else {
return false;
}
return true;
}
ValidateMethodFactory.validateDoubleRange = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getFloatValue(params[0]);
var p1 = ValidationFramework.getFloatValue(params[1]);
if (ValidateMethodFactory.validateInteger(field) || ValidateMethodFactory.validateDouble(field)) {
var v = parseFloat(obj.value);
return (v >= p0 && v <= p1);
} else {
return false;
}
return true;
}
ValidateMethodFactory.validateEqualsField = function(field, params) {
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
var formObj = document.getElementById(ValidationFramework._currentForm.getId());
try{
var eqField = formObj[params[0]];
}catch(e){
formObj = document.getElementById(FormToChk);
var eqField = formObj[params[0]];
}
if (eqField != null) {
return (obj.value == eqField.value)
} else {
return false;
}
}
ValidateMethodFactory.validateNumberRange = function(field, params) {//by lein
var obj = ValidationFramework.getWebFormFieldObj(field);
if (obj == null) return false;
if (obj.value == "") return true;
var p0 = ValidationFramework.getIntegerValue(params[0]);
var p1 = ValidationFramework.getIntegerValue(params[1]);
var v = parseFloat(obj.value);
if(isNaN(v)||v.length<1){
return false
}
return (v >= p0 && v <= p1);
}
ValidateMethodFactory.__isValidDate = function(day, month, year) {
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
if ((month == 4 || month == 6 || month == 9 || month == 11) &&(day == 31))
return false;
if (month == 2) {
var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day == 29 && !leap)) return false;
}
return true;
}
/**
* Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
* http://javascript.internet.com
*/
ValidateMethodFactory.__checkEmail = function(emailStr) {
if (emailStr.length == 0) {
return true;
}
var emailPat=/^(.+)@(.+)$/;
var specialChars="/(/)<>@,;:///"/./[/]";
var validChars="[^/s" + specialChars + "]";
var quotedUser="("[^"]*")";
var ipDomainPat=/^(d{1,3})[.](d{1,3})[.](d{1,3})[.](d{1,3})$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(/." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(/." + atom + ")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray[i] > 255) {
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1].length < 2) ||
(domArr[domArr.length-1].length > 3)) {
return false;
}
if (len < 2) {
return false;
}
return true;
}
////Language Definitions
var ValidationErrorString = new Object();
////Simplified Chinese(zh-ch)
ValidationErrorString["zh-cn"] = new Object();
ValidationErrorString["zh-cn"]["default"]="域{0}校验失败。";
ValidationErrorString["zh-cn"]["required"]="{0}不能为空。";
ValidationErrorString["zh-cn"]["integer"]="{0}必须是一个整数。";
ValidationErrorString["zh-cn"]["double"]="{0}必须是一个浮点数(带小数点)。";
ValidationErrorString["zh-cn"]["commonchar"] = "{0}必须是普通英文字符:字母,数字和下划线。";
ValidationErrorString["zh-cn"]["chinesechar"] = "{0}必须是中文字符。";
ValidationErrorString["zh-cn"]["minlength"]="{0}长度不能小于{1}个字符。";
ValidationErrorString["zh-cn"]["maxlength"]="{0}长度不能大于{1}个字符。" ;
ValidationErrorString["zh-cn"]["invalid"]="{0}无效。";
ValidationErrorString["zh-cn"]["date"]="{0}不是一个有效日期,期待格式:{1}。";
ValidationErrorString["zh-cn"]["integerrange"]="{0}必须在整数{1}和{2}之间。";
ValidationErrorString["zh-cn"]["doublerange"]="{0}必须在浮点数{1}和{2}之间。";
ValidationErrorString["zh-cn"]["pid"]="{0}不是一个有效身份证号。";
ValidationErrorString["zh-cn"]["email"]="{0}不是一个有效的Email。";
ValidationErrorString["zh-cn"]["equalsfield"]="{0}必须和{1}一致。";
ValidationErrorString["zh-cn"]["numberrange"]="{0}必须在数值{1}和{2}之间。";
////English(en-us)
ValidationErrorString["en-us"] = new Object();
ValidationErrorString["en-us"]["default"]="Failed when validating filed {0}.";
ValidationErrorString["en-us"]["required"]="{0} is required.";
ValidationErrorString["en-us"]["integer"]="{0} must be a integer.";
ValidationErrorString["en-us"]["double"]="{0} must be a double value. ";
ValidationErrorString["en-us"]["commonchar"] = "{0} should be common ascii characters, A-Z,a-z and undercore. ";
ValidationErrorString["en-us"]["chinesechar"] = "{0} must be chinese characters. ";
ValidationErrorString["en-us"]["minlength"]="{0} cannot be less then {1}. ";
ValidationErrorString["en-us"]["maxlength"]="{0} cannot be more then {1}. ";
ValidationErrorString["en-us"]["invalid"]="{0} in invalid. ";
ValidationErrorString["en-us"]["date"]="{0} is not an invalid date format: {1}. ";
ValidationErrorString["en-us"]["integerrange"]="{0} should be between number {1} and {2}. ";
ValidationErrorString["en-us"]["doublerange"]="{0} should be between double {1} and {2}. ";
ValidationErrorString["en-us"]["pid"]="{0} is not an valid pid. ";
ValidationErrorString["en-us"]["email"]="{0} is not an valid email address. ";
ValidationErrorString["en-us"]["equalsfield"]="{0} should be agree with field {1}. ";
ValidationErrorString["en-us"]["numberrange"]="{0} should be between number {1} and {2}. ";
ValidationErrorString["zh-big5"] = new Object();
ValidationErrorString["zh-big5"]["default"]="域{0}校驗失敗。";
ValidationErrorString["zh-big5"]["required"]="{0}不能為空。";
ValidationErrorString["zh-big5"]["integer"]="{0}必須是一個整數。";
ValidationErrorString["zh-big5"]["double"]="{0}必須是一個浮點數(帶小數點)。";
ValidationErrorString["zh-big5"]["commonchar"] = "{0}必須是普通英文字元:字母,數位和下劃線。";
ValidationErrorString["zh-big5"]["chinesechar"] = "{0}必須是中文字元。";
ValidationErrorString["zh-big5"]["minlength"]="{0}長度不能小於{1}個字元。";
ValidationErrorString["zh-big5"]["maxlength"]="{0}長度不能大於{1}個字元。" ;
ValidationErrorString["zh-big5"]["invalid"]="{0}無效。";
ValidationErrorString["zh-big5"]["date"]="{0}不是一個有效日期,期待格式:{1}。";
ValidationErrorString["zh-big5"]["integerrange"]="{0}必須在整數{1}和{2}之間。";
ValidationErrorString["zh-big5"]["doublerange"]="{0}必須在浮點數{1}和{2}之間。";
ValidationErrorString["zh-big5"]["pid"]="{0}不是一個有效身份證號。";
ValidationErrorString["zh-big5"]["email"]="{0}不是一個有效的Email。";
ValidationErrorString["zh-big5"]["equalsfield"]="{0}必須和{1}一致。";
ValidationErrorString["zh-big5"]["numberrange"]="{0}必須在數值{1}和{2}之間。";
// preload the validation file.
//ValidationFramework.getDocumentElement();