Javascript脚本收集(一):字符串相关

以下脚本是从项目中摘出来的。
function  DisableNumbers(fld, e, allowSpace)
{
    
var space=''
    
if(allowSpace!=null && allowSpace==true)
        space
=' ';
    
var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space;
    
var aux = aux2 = '';
    
var whichCode = (window.Event) ? e.which : e.keyCode;
   
if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode);  // Get key value from key code
    if (strCheck.indexOf(key) == -1)
        
return false;  // Not a valid key
    return true;
}


function  DisableNonNumericCharacters(fld, e, allowSpace,isDouble)
{
    
var space='';
    
var strCheck;
    
if(allowSpace!=null && allowSpace==true)
        space
=' ';
    
if (isDouble !=null && isDouble == true)
     strCheck 
= '0123456789.' + space;
    
else
     strCheck 
= '0123456789+ space;
     
    
var aux = aux2 = '';
    
var whichCode = (window.Event) ? e.which : e.keyCode;
   
if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode);  // Get key value from key code
    if (strCheck.indexOf(key) == -1)
        
return false;  // Not a valid key
    return true;
}


function  CapFirstLetter(fld)
{
    fld.value
=fld.value.properCase();
}


// TextControl.Attributes.Add("onkeypress", "return DisableSpecialCharacters(this,event,'',false,true)")
function  DisableSpecialCharacters(fld,e,allowedChars,spaceAllowed,numAllowed)
{
    
var space=' ';
    
//by default space is allowed
    if(spaceAllowed!=null && spaceAllowed==false)
        space
='';
    
//by default numbers r not allowed
    var num='';
    
if(numAllowed!=null && numAllowed==true)
        num
='0123456789';
        
    
var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;
    
var aux = aux2 = '';
    
var whichCode = (window.Event) ? e.which : e.keyCode;
    
if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode);  // Get key value from key code
    if (strCheck.indexOf(key) == -1)
        
return false;  // Not a valid key
    return true;
}


function  TrimText(txt)
{
    
return txt.replace(/^\s*|\s*$/g,"");
}


function  FormatNumber(nStr)
{
    
//add commas to number
    nStr += '';
    x 
= nStr.split('.');
    x1 
= x[0];
    x2 
= x.length > 1 ? '.' + x[1] : '';
    
var rgx = /(\d+)(\d{3})/;
    
while (rgx.test(x1)) {
        x1 
= x1.replace(rgx, '$1+ ',' + '$2');
    }

    
return x1 + x2;
}


function  RemoveCommas(str)
{
    
return str.toString().replace(new RegExp(/,/g), "");
}


function  IsEmptyString(str)
{
    
if (TrimText(str).length == 0)
        
return true;
    
else
        
return false;
}


function  IsEmailValid(str) 
{
        
        
var at="@"
        
var dot="."
        
var lat=str.indexOf(at)
        
var lstr=str.length
        
var ldot=str.indexOf(dot)
        
if (str.indexOf(at)==-1){
          
return false
        }


        
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
          
return false
        }


        
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
           
return false
        }


         
if (str.indexOf(at,(lat+1))!=-1){
            
return false
         }


         
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            
return false
         }


         
if (str.indexOf(dot,(lat+2))==-1){
            
return false
         }

        
         
if (str.indexOf(" ")!=-1){
            
return false
         }


          
return true                    
    }


function  ValidateNonNumericField(fld, e, allowSpace, isDouble, errorMessage)
{
    
var space='';
    
var strCheck;
    
if(allowSpace!=null && allowSpace==true)
        space
=' ';
    
if (isDouble !=null && isDouble == true)
     strCheck 
= '0123456789.' + space;
    
else
     strCheck 
= '0123456789+ space;
     
    
for (i = 0; i < fld.value.length; i++)
    
{
        key 
= fld.value.charAt(i);  // Get key value from key code
        if (strCheck.indexOf(key) == -1)
        
{
            alert(errorMessage);
            fld.value 
= '';
            fld.focus();
            
return;
        }

    }

}


function  ValidateSpecialCharactersField(fld,e,allowedChars,spaceAllowed,numAllowed,errorMessage)
{
    
var space=' ';
    
//by default space is allowed
    if(spaceAllowed!=null && spaceAllowed==false)
        space
='';
    
//by default numbers r not allowed
    var num='';
    
if(numAllowed!=null && numAllowed==true)
        num
='0123456789';
        
    
var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;

    
for (i = 0; i < fld.value.length; i++)
    
{
        key 
= fld.value.charAt(i);  // Get key value from key code
        if (strCheck.indexOf(key) == -1)
        
{
            alert(errorMessage);
            fld.value 
= '';
            fld.focus();
            
return;
        }

    }

}


//  var test = Round(15.1356, 2);
//
 Result: test = 15.14
function  Round(a_Num , a_Bit)
{
    
return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit));
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值