JS学习笔记3 TextUtil.js

来源:互联网 发布:如何在电脑上安装java 编辑:程序博客网 时间:2024/05/02 03:02

//文本对象
var TextUtil = new Object();

   
//文本框最大长度
 TextUtil.isNotMax = function(oTextArea){
  
return oTextArea.value.length!=oTextArea.getAttribute("maxlength");
 }

 
 
 
//阻止无效的字符
 TextUtil.blockChars = function(oTextbox,oEvent){
  oEvent 
= window.event;
  
var sInvalidChars = oTextbox.getAttribute("invalidchars");
  
var sChar = String.fromCharCode(oEvent.keyCode);

  
var bIsValidChar = sInvalidChars.indexOf(sChar) == -1;
  
  
return bIsValidChar 
  
 }

 
 
 
//允许有效的字符
  TextUtil.allowChars = function(oTextbox,oEvent){
  oEvent 
= window.event;
  
var sInvalidChars = oTextbox.getAttribute("validchars");
  
var sChar = String.fromCharCode(oEvent.keyCode);

  
var bIsValidChar = sInvalidChars.indexOf(sChar) > -1;
  
  
return bIsValidChar 
  
 }

 
 
//使用上下键操作数字
 TextUtil.numericScroll = function(oTextbox,oEvent){
   oEvent 
= window.event;
   
var iValue = oTextbox.value.length == 0 ? 0 :parseInt(oTextbox.value);
   
   
var iMax = oTextbox.getAttribute("max");
   
var iMin = oTextbox.getAttribute("min");
   
   
if(oEvent.keyCode == 38 ){
    
if(iMax == null || iValue<parseInt(iMax)){
    oTextbox.value 
= (iValue + 1);}

   }

   
else if (oEvent.keyCode == 40){
    
if(iMin == null || iValue > parseInt(iMin)){
    oTextbox.value 
= (iValue - 1);}

   }

 }

 
 TextUtil.autosuggestMatch 
= function(sText,arrValues){
   
var arrResult = new Array;
   
   
if(sText !=""){
    
for(var i=0;i<arrValues.length;i++){
      
if(arrValues[i].indexOf(sText)==0){
       arrResult.push(arrValues[i]);
      }

     }

    }

    
return arrResult;
 }

 TextUtil.autosuggest 
= function(oTextbox,arrValues,sListboxId){
  
var oListbox = document.getElementById(sListboxId);
  ListUtil.clear(oListbox);
  
var arrMatches = TextUtil.autosuggestMatch(oTextbox.value,arrValues);
  
  
for(var i=0;i<arrMatches.length;i++){
   ListUtil.add(oListbox,arrMatches[i]);
  }

 }