摘要:JavaScript:匿名函式陣列
搭配我的上一章JavaScript:自製-常用驗證函式
要呼叫一些驗證函式。
以下是我的測試函式,以及使用的匿名函式使用方式。請參考。
<script language ="javascript" type="text/javascript" > function ValidDate() { var strAlert =""; strAlert = CheckDate('TestDate','日期測試',true); ControlAlert(strAlert); } function ValidDateRegin() { var strAlert =""; strAlert = CheckDateRange ('TestBegDate','TestEndDate',"日期測試",true); ControlAlert(strAlert); } function ValidLength() { var strAlert =""; strAlert = CheckLen('TestLen',10,"長度測試",true); ControlAlert(strAlert); } function ValidNumber() { var strAlert =""; strAlert = CheckNumber('TestNumber',"數字測試",true); ControlAlert(strAlert); } function ValidInteger() { var strAlert =""; strAlert = CheckInteger('TestInteger',"整數測試",true); ControlAlert(strAlert); } function ValidEmail() { var strAlert =""; strAlert = CheckEmail('TestEmail',"E-mail測試",true); ControlAlert(strAlert); } function ValidIdNumber() { var strAlert =""; strAlert = CheckIdNumber('TestIdNumber',"IdNumber測試",true); ControlAlert(strAlert); } function ControlAlert(strAlert) { if(strAlert!="") alert(strAlert); else alert("成功"); } function ValidAll() { var i ; for(i=0;i<ValidFunctions.length;i++) { ValidFunctions[i](); } } var ValidFunctions = [] ; ValidFunctions[ValidFunctions.length] = function(){ValidDateRegin();} ValidFunctions[ValidFunctions.length] = function(){ValidLength();} ValidFunctions[ValidFunctions.length] = function(){ValidNumber();} ValidFunctions[ValidFunctions.length] = function(){ValidInteger();} ValidFunctions[ValidFunctions.length] = function(){ValidEmail();} ValidFunctions[ValidFunctions.length] = function(){ValidIdNumber();} </script>