?? validation.js
字號(hào):
/*********************************************************************
multi-language validation script
version 2.21
by matthew frank
There are no warranties expressed or implied. I will not be held
responsible for any loss of data or sanity through the use or
implementation of this script. This script may be re-used and
distrubted freely provided this header remains intact and all
supporting files are included (unaltered) in the distribution:
validation.js - this file
validation.htm - example form
readme.htm - directions on using this script
(validation.zip contains all files)
If you are interested in keeping up with the latest releases of this
script or asking questions about its implementation, think about joining
the eGroups discussion forum dedicated to data validation:
http://www.egroups.com/group/validation
*********************************************************************/
/*====================================================================
Function: Err
Purpose: Custom object constructor
Inputs: None
Returns: undefined
====================================================================*/
function Err(){
/*********************************************************************
Method: Err.clear
Purpose: Clear values from Error object
Inputs: none
Returns: undefined
*********************************************************************/
this.clear=function (){
this.source=new Object;
this.type=new Object;
this.format=new String;
}
/*********************************************************************
Method: Err.add
Purpose: Adds error to Error object
Inputs: oSource - source element object
vType - integer value of error type (or custom string)
sFormat - optional date format
Returns: undefined
*********************************************************************/
this.add=function (oSource,vType,sFormat){
this.source=oSource;
this.type=vType;
this.format=sFormat;
}
/*********************************************************************
Method: Err.raise
Purpose: Gives visual warning to user about all errors contained in
the Error object
Inputs: none
Returns: undefined
*********************************************************************/
this.raise=function (){
var oElement=this.source;
var sLang;
var sNym=oElement.getAttribute("nym");
// if type is not a number, it must be a custom error message
var sMsg=(typeof this.type=="string")?this.type:oElement.getAttribute("msg");
oElement.paint();
if(oElement.select)
oElement.select();
if(sMsg)
alert(sMsg);
else{
// Walk through object hierarchy to find applicable language
var oParent=oElement;
sLang=oParent.getAttribute("lang").substring(0,2).toLowerCase();
while(!sLang || !_validation.messages[sLang]){
oParent=oParent.parentElement;
if(oParent)
sLang=oParent.getAttribute("lang").substring(0,2).toLowerCase();
else
// Default language is Chinese
sLang="cn";
}
sMsg=_validation.messages[sLang][this.type];
alert(((sNym)?sNym+": ":"")+sMsg+((this.format)
?" "+this.format.reformat(sLang,this.type):""));
}
// Perform onvalidatefocus event handler for invalid field
if(oElement.onvalidatefocus)
oElement.onvalidatefocus();
// Give invalid field focus
oElement.focus();
// Clear the Err object
this.clear();
}
// Define the working object model
this.clear();
}
/*====================================================================
Function: Validation
Purpose: Custom object constructor.
Inputs: None
Returns: undefined
====================================================================*/
function Validation(){
// Define global constants for calls to error message arrays
this.REQUIRED = 0;
this.INTEGER = 1;
this.FLOAT = 2;
this.DATE = 3;
this.AMOUNT = 4;
this.MASK = 5;
this.TIMECODE = 6;
// Create error message dictionary
this.messages = new Array;
// Prototype the date tokens for each language
Array.prototype.MM = new String;
Array.prototype.DD = new String;
Array.prototype.YYYY = new String;
//English
this.messages["en"]=new Array(
"Please enter a value",
"Please enter a valid integer",
"Please enter a valid floating point",
"Please enter a valid date",
"Please enter a valid monetary amount",
"Please enter a value in the form of ");
with(this.messages["en"]){
MM="MM";
DD="DD";
YYYY="YYYY";
}
//Chinese
this.messages["cn"]=new Array(
"請(qǐng)輸入該項(xiàng)目",
"請(qǐng)輸入一個(gè)有效的整數(shù)",
"請(qǐng)輸入一個(gè)有效的浮點(diǎn)數(shù)",
"請(qǐng)輸入一個(gè)有效的日期",
"請(qǐng)輸入有效的金額",
"請(qǐng)按照以下的形式輸入:",
"請(qǐng)輸入一個(gè)有效的時(shí)碼,按照以下的形式:");
with(this.messages["cn"]){
YYYY="YYYY";
MM="MM";
DD="DD";
}
/*********************************************************************
Method: Validation.setDefault
Purpose: Set value for variable v if v is zero, empty string or
undefined
Inputs: v - variable (passed by value)
d - default value
Returns: v or d
*********************************************************************/
this.setDefault=function (v, d){
return (v)?v:d;
}
/*********************************************************************
Method: Validation.isDate
Purpose: Check that value is a date of the correct format
Inputs: oElement - form element
sFormat - string format
Returns: boolean
*********************************************************************/
this.isDate=function (oElement,sFormat){
var sDate=oElement.value;
var aDaysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
sDate = sDate.replace("年","-");
sDate = sDate.replace("月","-");
sDate = sDate.replace("日","");
// Fetch the date separator from the user's input
var sSepDate=sDate.charAt(sDate.search(/\D/));
// Fetch the date separator from the format
var sSepFormat=sFormat.charAt(sFormat.search(/[^MDY]/i));
// Compare separators
if (sSepDate!=sSepFormat)
return false;
// Fetch the three pieces of the date from the user's input and the format
var aValueMDY=sDate.split(sSepDate);
var aFormatMDY=sFormat.split(sSepFormat);
var iMonth,iDay,iYear;
// Validate that all pieces of the date are numbers
if ( !_validation.isNum(aValueMDY[0])
||!_validation.isNum(aValueMDY[1])
||!_validation.isNum(aValueMDY[2]))
return false;
// Assign day, month, year based on format
switch (aFormatMDY[0].toUpperCase()){
case "YYYY" :
iYear=aValueMDY[0];
break;
case "DD" :
iDay=aValueMDY[0];
break;
case "MM" :
iMonth=aValueMDY[0];
break;
default :
return false;
}
switch (aFormatMDY[1].toUpperCase()){
case "YYYY" :
iYear=aValueMDY[1];
break;
case "MM" :
iMonth=aValueMDY[1];
break;
case "DD" :
iDay=aValueMDY[1];
break;
default :
return false;
}
switch(aFormatMDY[2].toUpperCase()){
case "MM" :
iMonth=aValueMDY[2];
break;
case "DD" :
iDay=aValueMDY[2];
break;
case "YYYY" :
iYear=aValueMDY[2];
break;
default :
return false;
}
// Require 4 digit year
if(oElement.form.getAttribute("year4")!=null && iYear.length!=4)
return false;
// Process pivot date and update field
var iPivot=_validation.setDefault(oElement.getAttribute("pivot"),
oElement.form.getAttribute("pivot"));
if(iPivot && iPivot.length==2 && iYear.length==2){
iYear=((iYear>iPivot)?19:20).toString()+iYear;
var sValue=aFormatMDY.join(sSepFormat).replace(/MM/i,iMonth);
sValue=sValue.replace(/DD/i,iDay).replace(/YYYY/i,iYear);
oElement.value=sValue;
}
// Check for leap year
var iDaysInMonth=(iMonth!=2)?aDaysInMonth[iMonth-1]:
((iYear%4==0 && iYear%100!=0 || iYear % 400==0)?29:28);
return (iDay!=null && iMonth!=null && iYear!=null
&& iMonth<13 && iMonth>0 && iDay>0 && iDay<=iDaysInMonth);
}
/********************************************
Method: Validation.isTimeCode
Purpose: Check that parameter is a TimeCode
Inputs: oElement - form element
sFormat - string format
Returns: boolean
********************************************/
this.isTimeCode=function (oElement,sFormat){
var sTimeCode=oElement.value;
// Fetch the date separator from the user's input
var sSepDate=sTimeCode.charAt(sTimeCode.search(/\D/));
// Fetch the date separator from the format
var sSepFormat=sFormat.charAt(sFormat.search(/[^HMSF]/i));
// Compare separators
if (sSepDate!=sSepFormat)
return false;
// Fetch the three pieces of the date from the user's input and the format
var aValueHMSF=sTimeCode.split(sSepDate);
var aFormatHMSF=sFormat.split(sSepFormat);
var iHour,iMinute,iSecond,iFrame;
// Validate that all pieces of the timecode are numbers
if ( !_validation.isNum(aValueHMSF[0])
||!_validation.isNum(aValueHMSF[1])
||!_validation.isNum(aValueHMSF[2]))
return false;
// Assign hour, minute, second, frame based on format
switch (aFormatHMSF[0].toUpperCase()){
case "HH" :
iHour=aValueHMSF[0];
break;
case "MM" :
iMinute=aValueHMSF[0];
break;
case "SS" :
iSecond=aValueHMSF[0];
break;
case "FF" :
iFrame=aValueHMSF[0];
break;
default :
return false;
}
switch (aFormatHMSF[1].toUpperCase()){
case "HH" :
iHour=aValueHMSF[1];
break;
case "MM" :
iMinute=aValueHMSF[1];
break;
case "SS" :
iSecond=aValueHMSF[1];
break;
case "FF" :
iFrame=aValueHMSF[1];
break;
default :
return false;
}
switch(aFormatHMSF[2].toUpperCase()){
case "HH" :
iHour=aValueHMSF[2];
break;
case "MM" :
iMonth=aValueHMSF[2];
break;
case "SS" :
iSecond=aValueHMSF[2];
break;
case "FF" :
iFrame=aValueHMSF[2];
break;
default :
return false;
}
switch(aFormatHMSF[3].toUpperCase()){
case "HH" :
iHour=aValueHMSF[3];
break;
case "MM" :
iMonth=aValueHMSF[3];
break;
case "SS" :
iSecond=aValueHMSF[3];
break;
case "FF" :
iFrame=aValueHMSF[3];
break;
default :
return false;
}
return (iHour!=null && iMinute!=null && iSecond!=null && iFrame!=null
&& iHour<24 && iHour>=0 && iMinute>=0 && iMinute<60 && iSecond>=0 && iSecond<60 && iFrame>=0 && iFrame<25);
}
/********************************************
Method: Validation.isNum
Purpose: Check that parameter is a number
Inputs: v - string value
Returns: boolean
********************************************/
this.isNum=function (v){
return (typeof v!="undefined" && v.toString() && !/\D/.test(v));
}
/*********************************************************************
Method: Validation.setup
Purpose: Set up methods and event handlers for all forms and elements
Inputs: none
Returns: undefined
*********************************************************************/
this.setup=function (){
// Fan through forms on page to perform initializations
var i,iForms=document.forms.length;
for(i=0; i<iForms; i++){
var oForm=document.forms[i];
if(!oForm.bProcessed){
/*********************************************
Method: Form.markRequired
Purpose: Mark all required fields for a form
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -