客戶端腳本容錯處理

// 為 Array 類增加一個 max 方法
Array.prototype.max = function()
{
    
var i, max = this[0];
    
    
for( i = 1; i < this.length; i++ )
    
{
        
if( max < this[i] )
        max 
= this[i];
    }

    
    
return max;
}


// 為 String 類增加一個 trim 方法
String.prototype.trim = function()
{
    
// 用正則表達式將前後空格用空字符串替代。
    return this.replace( /(^\s*)|(\s*$)/g, "" );
}


// 使用正則表達式,檢測 s 是否滿足模式 re
function checkExp( re, s )
{
    
return re.test( s );
}


// 驗證是否 字母數字
function isAlphaNumeric( strValue )
{
    
// 只能是 A-Z a-z 0-9 之間的字母數字 或者為空
    return checkExp( /^\w*$/gi, strValue );
}


// 驗證是否 日期
function isDate( strValue )
{
    
// 日期格式必須是 2001-10-1/2001-1-10 或者為空
    if( isEmpty( strValue ) ) return true;

    
if!checkExp( /^\d{4}-[01]?\d-[0-3]?\d$/g, strValue ) ) return false;
    
// 或者 /^\d{4}-[1-12]-[1-31]\d$/
    
    
var arr = strValue.split( "-" );
    
var year = arr[0];
    
var month = arr[1];
    
var day = arr[2];
    
    
// 1 <= 月份 <= 12,1 <= 日期 <= 31
    if!( ( 1<= month ) && ( 12 >= month ) && ( 31 >= day ) && ( 1 <= day ) ) )
        
return false;
        
    
// 潤年檢查
    if!( ( year % 4 ) == 0 ) && ( month == 2&& ( day == 29 ) )
        
return false;
    
    
// 7月以前的雙月每月不超過30天
    if( ( month <= 7 ) && ( ( month % 2 ) == 0 ) && ( day >= 31 ) )
        
return false;
    
    
// 8月以後的單月每月不超過30天
    if( ( month >= 8&& ( ( month % 2 ) == 1&& ( day >= 31 ) )
        
return false;
    
    
// 2月最多29天
    if( ( month == 2&& ( day >=30 ) )
        
return false;
    
    
return true;
}


// 驗證是否 Email
function isEmail( strValue )
{
    
// Email 必須是 x@a.b.c.d 等格式 或者為空
    if( isEmpty( strValue ) ) return true;
    
    
//return checkExp( /^\w+@(\w+\.)+\w+$/gi, strValue );    //2001.12.24測試出錯 檢查 jxj-xxx@114online.com時不能通過
    //Modify By Tianjincat 2001.12.24
    var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
    
return checkExp( pattern, strValue );
    
}


// 驗證是否 為空
function isEmpty( strValue )
{
    
if( strValue == "" )
        
return true;
    
else
        
return false;
}


// 驗證是否 數字
function isNumeric( strValue )
{
    
// 數字必須是 0123456789 或者為空
    
    
return checkExp( /^\d*$/g, strValue );
}


// 驗證是否 貨幣
function isMoney( strValue )
{
    
// 貨幣必須是 -12,345,678.9 等格式 或者為空
    if( isEmpty( strValue ) ) return true;
    
    
return checkExp( /^[+-]?\d+(,\d{3})*(\.\d+)?$/g, strValue );
}


// 驗證是否 電話
function isPhone( strValue )
{
    
// 普通電話    (0755)4477377-3301/(86755)6645798-665
    // Call 機    95952-351
    // 手機        130/131/135/136/137/138/13912345678
    // 或者為空
    if( isEmpty( strValue ) ) return true;
    
    
return checkExp( /(^\(\d{3,5}\)\d{6,8}(-\d{2,8})?$)|(^\d+-\d+$)|(^(130|131|135|136|137|138|139)\d{8}$)/g, strValue );
}


// 驗證是否 郵政編碼
function isPostalCode( strValue )
{
    
// 郵政編碼必須是6位數字
    return checkExp( /(^$)|(^\d{6}$)/gi, strValue )
}


// 驗證是否 URL
function isURL( strValue )
{
    
// http://www.yysoft.com/ssj/default.asp?Type=1&ArticleID=789
    if( isEmpty( strValue ) ) return true;
    
    
var pattern = /^(http|https|ftp):\/\/(\w+\.)+[a-z]{2,3}(\/\w+)*(\/\w+\.\w+)*(\?\w+=\w*(&\w+=\w*)*)*/gi;
    
// var pattern = /^(http|https|ftp):(\/\/|\\\\)(\w+\.)+(net|com|cn|org|cc|tv|[0-9]{1,3})((\/|\\)[~]?(\w+(\.|\,)?

\w\
/)*([?]\w+[=])*\w+(\&\w+[=]\w+)*)*$/gi;
    
// var pattern = ((http|https|ftp):(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(((\/[\~]*|\\[\~]*)(\w)

+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)/gi;

    
return checkExp( pattern, strValue );
    
}


// 檢查字段長度
//
//
    strValue    字符串
//
    strParam    檢查參數,形如:L<10, L=5, L>117
//
function checkLength( strValue, strParam )
{
    
if( isEmpty( strValue ) )    return true;
    
    
// 參數形如:L<10, L=5, L>117
    if( strParam.charAt( 0 ) != 'L' )    return false;
    
    
var l = strValue.length;
    
var ml = parseInt( strParam.substr( 2 ) );
    
    
switch( strParam.charAt( 1 ) )
    
{
        
case '<' :
            
if( l >= ml )
                
return false;
            
break;
            
        
case '=' :
            
if( l != ml )
                
return false;
            
break;
            
        
case '>' :
            
if( l <= ml )
                
return false;
            
break;
            
        
default :
            
return false
    }

    
    
return true;
}


// 檢查輸入數據長度的合法性(字符長度不能大於**個字符)
//
//
    輸入參數
//
        strName        字段對象
//
        strDescription    字段描述
//
        strLength    字段長度
//
function ValidateMaxLength( strName, strDescription, strLength) {
    
var strMsg = "";
    
var strValue = document.all( strName ).value.trim();
    
var strMaxLength = "L<" + strLength;
    
if!checkLength( strValue, strMaxLength ))
    strMsg 
= '"' + strDescription + '" 必須小於'+ strLength + '個字符\n';
    
return strMsg;
}


// 檢查輸入數據長度的合法性(字符長度不能小於**個字符)
//
//
    輸入參數
//
        strName        字段對象
//
        strDescription    字段描述
//
        strLength    字段長度
//
function ValidateMinLength( strName, strDescription, strLength) {
    
var strMsg = "";
    
var strValue = document.all( strName ).value.trim();
    
var strMaxLength = "L>" + strLength;
    
if!checkLength( strValue, strMaxLength ))
    strMsg 
= '"' + strDescription + '" 必須大於'+ (parseInt(strLength)+1+ '個字符\n';
    
return strMsg;
}


// 檢查輸入數據長度的合法性(字符長度等於**個字符)
//
//
    輸入參數
//
        strName        字段對象
//
        strDescription    字段描述
//
        strLength    字段長度
//
function ValidateEquLength( strName, strDescription, strLength) {
    
var strMsg = "";
    
var strValue = document.all( strName ).value.trim();
    
var strMaxLength = "L=" + strLength;
    
if!checkLength( strValue, strMaxLength ))
    strMsg 
= '"' + strDescription + '" 必須等於'+ strLength + '個字符\n';
    
return strMsg;
}


// 檢查輸入數據的合法性(應用在離開字段時)
//
//
    輸入參數
//
        obj        字段對象
//
        strDescription    字段描述
//
        strType    字段類型
//
function CheckValid( obj, strDescription, strType)
{
    
var strMsg = "";
    
var strValue = obj.value.trim();
    
    
switch( strType )
    
{
        
case "AlphaNumeric" :    // 字母數字
            if!isAlphaNumeric( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是字母或數字!\n';
            
break;
            
        
case "Date" :    // 日期
            if!isDate( strValue ) ) 
                strMsg 
= '"' + strDescription + '" 必須具有正確的日期格式,如 2001-10-01\n';
            
break;
                
        
case "Email" :    // 電子郵件
            if!isEmail( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須具有正確的郵件格式,如 xx@yy.com\n';
            
break;
                
        
case "NotEmpty" :    // 不許空值
            if( isEmpty( strValue ) )
                strMsg 
= '"' + strDescription + '" 不能為空!\n';
            
break;
                
        
case "Numeric" :    //數字
            if!isNumeric( strValue )  )
                strMsg 
= '"' + strDescription + '" 必須是數字!\n';
            
break;
        
        
case "Money" :    //貨幣
            if!isMoney( strValue )  )
                strMsg 
= '"' + strDescription + '" 必須具有正確的貨幣格式,如 -123,456.789\n';
            
break;
                    
        
case "Phone" :    // 電話
            if!isPhone( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須具有正確的電話格式,如 (0755)1234567-999\n';
            
break;
            
        
case "PostalCode" :    // 郵政編碼
            if!isPostalCode( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是6位數字!\n';
            
break;
            
        
case "URL" :    // URL
            if!isURL( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是正確的URL格式!\n';
            
break;
                
        
default :    // 其他
            if( arrType[i].charAt( 0 ) == 'L' )
            
{
                
if!checkLength( strValue, arrType[i] ) )
                    strMsg 
= '"' + strDescription + '" 的長度必須 ' + arrType[i].substr(1+ '\n';
            }

            
else
                strMsg 
= '錯誤:"' + strDescription + '" 的類型 "' + strType + '" 不能識別!\n';
    }

    
    
if( strMsg != "" ) 
    
{
        window.alert( strMsg );
        obj.focus();
    }

    
    
return;
}


// 驗證輸入數據的合法性
//
//
    輸入參數
//
        值
//
        strDescription    字段描述
//
        strType    字段類型
//
//
    輸出參數
//
        空串    通過驗證
//
        非空    未通過驗證
//
function ValidateKernel(strValue,strDescription,strType)
{
    
var strMsg = "";
    
var arrType = strType.split( " " );
    
    
forvar i = 0; i < arrType.length; i++ )
    
switch( arrType[i] )
    
{
        
case "AlphaNumeric" :    // 字母數字
            if!isAlphaNumeric( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是字母或數字!\n';
            
break;
        
        
case "Date" :    // 日期
            if!isDate( strValue ) ) 
                strMsg 
= '"' + strDescription + '" 必須具有正確的日期格式,如 2001-10-1\n';
            
break;
            
        
case "Email" :    // 電子郵件
            if!isEmail( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須具有正確的郵件格式,如 webmaster@yysoft.com\n';
            
break;
            
        
case "NotEmpty" :    // 不許空值
            if( isEmpty( strValue ) )
                strMsg 
= '"' + strDescription + '" 不能為空!\n';
            
break;
            
        
case "Numeric" :    //數字
            if!isNumeric( strValue )  )
                strMsg 
= '"' + strDescription + '" 必須是數字!\n';
            
break;
            
        
case "Money" :    //貨幣
            if!isMoney( strValue )  )
                strMsg 
= '"' + strDescription + '" 必須具有正確的貨幣格式,如 -123,456.789\n';
            
break;
                
        
case "Phone" :    // 電話
            if!isPhone( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須具有正確的電話格式,如 (0755)1234567-999\n';
            
break;
        
        
case "PostalCode" :    // 郵政編碼
            if!isPostalCode( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是6位數字!\n';
            
break;
            
        
case "URL" :    // URL
            if!isURL( strValue ) )
                strMsg 
= '"' + strDescription + '" 必須是正確的URL格式!\n';
            
break;
            
        
default :    // 其他
            if( arrType[i].charAt( 0 ) == 'L' )
            
{
                
if!checkLength( strValue, arrType[i] ) )
                    strMsg 
= '"' + strDescription + '" 的長度必須 ' + arrType[i].substr(1+ '\n';
            }

            
else
                strMsg 
= '錯誤:"' + strDescription + '" 的類型 "' + strType + '" 不能識別!\n';
    }


    
return strMsg;
}

// 驗證輸入數據的合法性
//
//
    輸入參數
//
        strName    字段名
//
        strDescription    字段描述
//
        strType    字段類型
//
//
    輸出參數
//
        空串    通過驗證
//
        非空    未通過驗證
//
function Validate( strName, strDescription, strType)
{
    
var strMsg = "";
    
var strValue = document.all( strName ).value.trim();

    strMsg 
= ValidateKernel(strValue,strDescription,strType);

    
return strMsg;
}

// 驗證輸入數據的合法性
//
//
    輸入參數
//
        fldVal 值
//
        strDescription    字段描述
//
        strType    字段類型
//
//
    輸出參數
//
        空串    通過驗證
//
        非空    未通過驗證
//
function ValidateFld( fldVal, strDescription, strType)
{
    
var strMsg = "";
    
var strValue = fldVal;

    strMsg 
= ValidateKernel(strValue,strDescription,strType);

    
return strMsg;
}



// 確認刪除
function confirm_delete( url )
{
    
if( confirm( "您確實要刪除嗎?" ) )
    
{
        window.location 
= ( url )
    }

}


// 鍊接轉向
function goToURL( url )
{
    window.location 
= url;
}


// 打開新窗口
function openNewWin( url, width, height )
{
    
var newwin = window.open( url, "NewWin"

"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + 

height 
+"" );
    newwin.focus();
    
return false;
}


// 對話框窗口
function openDialog( url, width, height)
{    
    showModalDialog( url, 
"NewWin","dialogWidth:"+ width +";dialogHeight:"+ height 

+";dialogTop:100;dialogLeft:200;status:no;");
}


//功能:checkBox的全選和全不選
var checkboxflag = "false";
function check(field) 
{
    
if (checkboxflag == "false"
    
{
        
if(field.length == null)    //處理可能只有一條記錄的Bug
        {
            
if (field.disabled != true)
            
{
                field.checked 
= true;
            }

        }

        
else
        
{
            
for (i = 0; i < field.length; i++
            
{
                
if (field[i].disabled != true)    //如果是disabled則不修改萁狀態 Modify By tianjincat 2002-03-

26
                
{
                    field[i].checked 
= true;
                }

            }

        }

        checkboxflag 
= "true";
        
return "全不選"
    }

    
else 
    
{
        
if(field.length == null)    //處理可能只有一條記錄的Bug
        {
            
if (field.disabled != true)
            
{
                field.checked 
= false;
            }

        }

        
else
        
{
            
for (i = 0; i < field.length; i++
            
{
                
if (field[i].disabled != true)    //如果是disabled則不修改萁狀態 Modify By tianjincat 2002-03-

26
                
{
                    field[i].checked 
= false
                }
    
            }

        }

        checkboxflag 
= "false";
        
return "全選"
    }

}


//功能:checkBox的反選擇
//
Added By tianjincat 2002-04-01
function chkinverse(field)
{
    
if(field.length == null)    //處理可能只有一條記錄的Bug
    {
        
if (field.disabled != true)
        
{
            
if(field.checked == true)
            
{
                field.checked 
= false;
            }

            
else
            
{
                field.checked 
= true;
            }

        }
    
    }

    
else
    
{
        
        
for(i = 0; i < field.length; i++)
        
{
            
if (field[i].disabled != true)
            
{
                
if(field[i].checked == true)
                
{
                    field[i].checked 
= false;
                }

                
else
                
{
                    field[i].checked 
= true;
                }
    
            }

        }

    }
    
    
return "反選"
}


//選擇記錄提示
//
form 提交的FORM名稱    msg  提示信息    field CheckBox的名稱
//
function ActionConfirm(form,msg,field) 
{
    
var flag=0;
    
var truthBeTold;

    
if(field==null)
        
return;

    
for(i = 0; i < field.length; i++)
        
{
            
if (field[i].disabled != true)
            
{
                
if(field[i].checked == true)
                
{
                    flag
=1;
                }

            }

        }


    
if(field.length == null)    //處理可能只有一條記錄的Bug
    {
        
if(field.checked == true)
        
{
            flag
=1;
        }
        
    }


    
if (flag==0)
    
{
        alert(
"請選擇記錄!");
        
if(typeof(form.actionId) != "undefined")
            form.actionId.value 
= "";
    }

    
else
    
{
        truthBeTold 
=window.confirm("你確定要["+msg+"]嗎?");
        
if (truthBeTold) {
            
//form.DoType.value=msg;
            form.submit();
            flag 
= 1;
        }
 
        
else
        
{
            
if(typeof(form.actionId) != "undefined")
                form.actionId.value 
= "";
        }

    }


}
使用:
<SCRIPT LANGUAGE="JavaScript">
<!--
function doSubmit(frm){
 var str = "";
 str = str + ValidateFld(frm.userid.value,"評論者id","NotEmpty");
 str = str + ValidateFld(frm.title.value,"評論標題","NotEmpty");
 str = str + ValidateFld(frm.content.value,"評論內容","NotEmpty");
 if(str != ""){
  alert(str);return false;
 }
 return confirm("確認提交評論麼?");
}
//-->
</SCRIPT>
onsubmit="return doSubmit(this)"

posted on 2006-10-21 08:25  ★金★  阅读(237)  评论(0编辑  收藏  举报

导航