JS常用函數彙總

//數字格式轉換成千分位 和 去除千分位函數
<script language="javascript" type="text/javascript">
/* 數字格式轉換成千分位
 *@param{Object}num
 */
function commafy(num){
   if((num+"").trim()==""){
      return "";
   }
   if(isNaN(num)){
      return "";
   }
   num = num+"";
   if(/^.*\..*$/.test(num)){
      varpointIndex =num.lastIndexOf(".");
      varintPart = num.substring(0,pointIndex);
      varpointPart =num.substring(pointIndex+1,num.length);
      intPart = intPart +"";
       var re =/(-?\d+)(\d{3})/;
       while(re.test(intPart)){
          intPart =intPart.replace(re,"$1,$2");
       }
      num = intPart+"."+pointPart;
   }else{
      num = num +"";
       var re =/(-?\d+)(\d{3})/;
       while(re.test(num)){
          num =num.replace(re,"$1,$2");
       }
   }
    return num;
}
/**
 * 去除千分位
 *@param{Object}num
 */
function delcommafy(num){
   if((num+"").trim()==""){
      return"";
   }
   num=num.replace(/,/gi,'');
   return num;
}
</script>
//最簡JS驗證碼
<script language="javascript" type="text/javascript">
var code ; //在全局 定義驗證碼
function createCode(){ 
code = "";
var codeLength = 4;//驗證碼的長度
var checkCode = document.getElementById("checkCodeOld");
checkCode.value = "";
var selectChar = new Array(2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
for ( var i = 0; i < codeLength; i++) {
var charIndex = Math.floor(Math.random() * 32);
code += selectChar[charIndex];
}
if (code.length != codeLength) {
createCode();
}
checkCode.value = code;
}
function validate() {
var inputCode = document.getElementById("checkcode").value.toUpperCase();
if (inputCode.length <= 0) {
alert("請輸入驗證碼!");
return false;
} else if (inputCode != code) {
alert("驗證碼輸入錯誤!");
createCode();
return false;
} else {
document.userlogin.submit();
}
}
</script>


發佈了30 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章