文件名:form.js
<%
//start for element
function elements(){
this.name = "";
this.value = "";
this.desc = "";
this.pattern = false;
this.maxv = false;
this.minv = false;
this.maxl = false;
this.minl = false;
this.required = false;
this.html = "";
this.comstring = "";
this.attr = "";
this.avai = true;
this.Init = function(strName,strValue){
this.name = strName;
this.value = strValue;
if(!isEmpty(Request.QueryString(strName))){
this.value = Request.QueryString(strName);
}
if(!isEmpty(Request.Form(strName))){
this.value = Request.Form(strName);
}
/*if(!isEmpty(Request.Cookies(strName))){
this.value = Request.Cookies(strName);
}
if(!isEmpty(Request.ServerVariables(strName))){
this.value = Request.ServerVariables(strName);
}
if(!isEmpty(Session(strName))){
this.value = Session(strName);
}
if(!isEmpty(Application(strName))){
this.value = Application(strName);
}*/
}
this.get = function(strVarname){
return eval("this."+strVarname);
}
this.set = function(strVarname,strVarvalue){
eval("this."+strVarname+"="+strVarvalue);
}
this.setDesc = function (strDesc){
this.desc = strDesc;
}
this.setPattern = function (strPattern){
this.pattern = eval("/"+strPattern+"/g");
}
this.setCom = function (strCom){
this.comstring = strCom;
}
this.setLLimit = function (intMin,intMax){
this.maxl = intMax;
this.minl = intMin;
}
this.setVLimit = function (intMin,intMax){
this.minv = intMin;
this.maxv = intMax;
}
this.setRequired = function(bRequired){
this.required = bRequired;
}
this.setAttr = function(strAttribute){
this.attr = strAttribute;
}
this.basicTest = function(){
if(this.required != false){
if(this.value.length == 0){
this.addErr(this.desc+"是必填项");
this.avai = false;
}
}
if(this.pattern != false){
if(!this.pattern.test(this.value)){
this.addErr(this.desc+"含有非法字符");
this.avai = false;
}
}
if(this.maxv != false){
if(this.value > this.maxv){
this.addErr(this.desc+"不能大于"+this.maxv);
this.avai = false;
}
}
if(this.minv != false){
if(this.value < this.minv){
this.addErr(this.desc+"不能小于"+this.minv);
this.avai = false;
}
}
if(this.minl != false){
if(this.value.length < this.minl){
this.addErr(this.desc+"长度不能小于"+this.minl);
this.avai = false;
}
}
if(this.maxl != false){
if(this.value.length > this.maxl){
this.addErr(this.desc+"长度不能大于"+this.maxl);
this.avai = false;
}
}
if(this.comstring.length != 0){
if(this.value != this.comstring){
this.addErr();
this.avai = false;
}
}
if(this.avai == false){
return false;
}else{
return true;
}
}
this.basicBuild = function(){
//nothing
}
}
elements.prototype = new basic;
//end for element
//start for input
function text(name,value){
this.base = elements;
this.type = "text";
this.textTest = function(){
if(this.basicTest() == false){
return false;
}
}
this.textBuild = function(){
this.basicBuild();
}
}
text.prototype = new elements;
//end for text
//start for select
function select(name,value){
this.base = elements;
this.type = "select";
this.value_array = new Array();
this.options_str = "";
//value mixed: string,explode with ","
this.buildValue = function(value){
var tmp = new Array();
if (typeof(value) == "object"){
for (var i=0; i<value.length; i++){
tmp[i] = new Array();
if(typeof(value[i]) == "string"){
tmp[i][0] = value[i];
tmp[i][1] = value[i];
}else{
tmp[i] = value[i];
}
}
this.value_array = tmp;
}else if(typeof(value) == "string"){
this.buildValue(value.split(","));
}
}
//build html
this.buildOptions = function(){
for (var i=0; i<this.value_array.length; i++){
this.options_str += "<option value="+this.value_array[i][0]+" ";
if(this.value.Count <= 1){
if(this.value_array[i][0] == this.value){
this.options_str +="selected";
}
}else{
tmpv = String(this.value);
tmpv = tmpv.split(",");
for (var j=0; j<tmpv.length; j++){
if ( this.value_array[i][0] == Trim(tmpv[j]) ){
this.options_str += "selected";
}
}
}
this.options_str +=">"+this.value_array[i][1]+"</option>/n";
}
}
this.selectTest = function(){
this.basicTest();
}
this.selectBuild = function(){
this.basicBuild();
this.buildOptions();
}
}
select.prototype = new elements;
//end for select
function radio(){
this.base = elements;
this.type="radio";
this.value_array = new Array();
this.radioTest = function(){
return this.basicTest();
}
this.radioBuild = function(){
this.basicBuild();
}
//
this.radioAssign = function(oTpl,fhandle,broken,len){
oTpl.setBlock(fhandle,this.name.toUpperCase(),this.name);
if (typeof(broken) == "undefined"){
broken = " ";
}
if (typeof(len) == "undefined"){
len = 1;
}
for (var i=0; i<this.value_array.length; i++){
oTpl.setVar("value",this.value_array[i][0]);
oTpl.setVar("text",this.value_array[i][1]);
if(this.value == this.value_array[i][0]){
oTpl.setVar("checked","checked");
}else{
oTpl.setVar("checked","");
}
if (i%len == (len-1)){
oTpl.setVar("broken",broken);
}else{
oTpl.setVar("broken","");
}
oTpl.parse(this.name,this.name.toUpperCase(),true);
}
}
this.buildValue = function(value){
var tmp = new Array();
if (typeof(value) == "object"){
for (var i=0; i<value.length; i++){
tmp[i] = new Array();
if(typeof(value[i]) == "string"){
tmp[i][0] = value[i];
tmp[i][1] = value[i];
}else{
tmp[i] = value[i];
}
}
this.value_array = tmp;
}else if(typeof(value) == "string"){
this.buildValue(value.split(","));
}
}
}
radio.prototype = new elements;
//end for radio
//start for checkbox
function checkbox(){
this.base = elements;
this.type="checkbox";
this.value_array = new Array();
this.checkboxTest = function(){
return this.basicTest();
}
this.checkboxBuild = function(){
this.basicBuild();
}
//
this.checkboxAssign = function(oTpl,fhandle,broken,len){
oTpl.setBlock(fhandle,this.name.toUpperCase(),this.name);
if (typeof(broken) == "undefined"){
broken = " ";
}
if (typeof(len) == "undefined"){
len = 1;
}
for (var i=0; i<this.value_array.length; i++){
oTpl.setVar("value",this.value_array[i][0]);
oTpl.setVar("text",this.value_array[i][1]);
if(this.value.Count <= 1){
if(this.value == this.value_array[i][0]){
oTpl.setVar("checked","checked");
}else{
oTpl.setVar("checked","");
}
}else{
tmpv = String(this.value);
tmpv = tmpv.split(",");
var checked = "";
for (var j=0; j<tmpv.length; j++){
if (this.value_array[i][0] == Trim(tmpv[j])){
checked = "checked";
}
}
oTpl.setVar("checked",checked);
}
//for broken
if (i%len == (len-1)){
oTpl.setVar("broken",broken);
}else{
oTpl.setVar("broken","");
}
oTpl.parse(this.name,this.name.toUpperCase(),true);
}
}
this.buildValue = function(value){
var tmp = new Array();
if (typeof(value) == "object"){
for (var i=0; i<value.length; i++){
tmp[i] = new Array();
if(typeof(value[i]) == "string"){
tmp[i][0] = value[i];
tmp[i][1] = value[i];
}else{
tmp[i] = value[i];
}
}
this.value_array = tmp;
}else if(typeof(value) == "string"){
this.buildValue(value.split(","));
}
}
}
checkbox.prototype = new elements;
//end for checkbox
//start for form
function form(fname){
this.name = fname;
this.action = "";
this.qstring = "";
this.method = "post";
this.elements = new Array();
this.htmlstr = "";
this.head = "";
this.foot = "</form>";
this.avai = true;
this.setAction = function(strAction){
this.action = strAction;
}
this.addElement = function(oElement){
this.elements.push(oElement);
}
this.formTest = function(){
for(var i=0;i<=this.elements.length - 1;i++){
if(eval("this.elements[i]."+this.elements[i].type+"Test()") == false){
this.addErr(this.elements[i].getErrorMsg());
this.avai = false;
}
}
return this.avai;
}
this.addQueryString = function(strParam,strValue){
//todo;
}
this.assignfortpl = function(oTpl){
this.formTest();
this.formBuild();
//打印表单错误信息
oTpl.setVar(this.name+"_errormsg",this.getErrorMsg());
for(var i=0;i<this.elements.length;i++){
oTpl.setVar(this.elements[i].name+"_value",this.elements[i].value);
if (this.elements[i].type == "select"){
oTpl.setVar(this.elements[i].name+"_options_str",this.elements[i].options_str);
}
}
}
this.formBuild = function(){
for(var i=0;i<=this.elements.length - 1;i++){
eval("this.elements[i]."+this.elements[i].type+"Build()");
}
}
}
form.prototype = new basic;
//end for form
//%>