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

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

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

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

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

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

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

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

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

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

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

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

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

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

 return true
          return true                    
 }
    }

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

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

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

 // var test = Round(15.1356, 2);
// var test = Round(15.1356, 2);
 // Result: test = 15.14
// Result: test = 15.14
 function Round(a_Num , a_Bit)
function Round(a_Num , a_Bit)
 {
{
 return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit));
    return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit));
 }
} 
 function DisableNumbers(fld, e, allowSpace)
function DisableNumbers(fld, e, allowSpace) {
{ var space=''
    var space='' if(allowSpace!=null && allowSpace==true)
    if(allowSpace!=null && allowSpace==true) space=' ';
        space=' '; var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space;
    var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space; var aux = aux2 = '';
    var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode;
    var whichCode = (window.Event) ? e.which : e.keyCode; if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true; key = String.fromCharCode(whichCode);  // Get key value from key code
    key = String.fromCharCode(whichCode);  // Get key value from key code if (strCheck.indexOf(key) == -1)
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
        return false;  // Not a valid key return true;
    return true; }
}
 function DisableNonNumericCharacters(fld, e, allowSpace,isDouble)
function DisableNonNumericCharacters(fld, e, allowSpace,isDouble) {
{ var space='';
    var space=''; var strCheck;
    var strCheck; if(allowSpace!=null && allowSpace==true)
    if(allowSpace!=null && allowSpace==true) space=' ';
        space=' '; if (isDouble !=null && isDouble == true)
    if (isDouble !=null && isDouble == true) strCheck = '0123456789.' + space;
     strCheck = '0123456789.' + space; else
    else strCheck = '0123456789' + space;
     strCheck = '0123456789' + space; 
      var aux = aux2 = '';
    var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode;
    var whichCode = (window.Event) ? e.which : e.keyCode; if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true; key = String.fromCharCode(whichCode);  // Get key value from key code
    key = String.fromCharCode(whichCode);  // Get key value from key code if (strCheck.indexOf(key) == -1)
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
        return false;  // Not a valid key return true;
    return true; }
}
 function CapFirstLetter(fld)
function CapFirstLetter(fld) {
{ fld.value=fld.value.properCase();
    fld.value=fld.value.properCase(); }
}
 //TextControl.Attributes.Add("onkeypress", "return DisableSpecialCharacters(this,event,'',false,true)")
//TextControl.Attributes.Add("onkeypress", "return DisableSpecialCharacters(this,event,'',false,true)") function DisableSpecialCharacters(fld,e,allowedChars,spaceAllowed,numAllowed)
function DisableSpecialCharacters(fld,e,allowedChars,spaceAllowed,numAllowed) {
{ var space=' ';
    var space=' '; //by default space is allowed
    //by default space is allowed if(spaceAllowed!=null && spaceAllowed==false)
    if(spaceAllowed!=null && spaceAllowed==false) space='';
        space=''; //by default numbers r not allowed
    //by default numbers r not allowed var num='';
    var num=''; if(numAllowed!=null && numAllowed==true)
    if(numAllowed!=null && numAllowed==true) num='0123456789';
        num='0123456789'; 
         var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;
    var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num; var aux = aux2 = '';
    var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode;
    var whichCode = (window.Event) ? e.which : e.keyCode; if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true; key = String.fromCharCode(whichCode);  // Get key value from key code
    key = String.fromCharCode(whichCode);  // Get key value from key code if (strCheck.indexOf(key) == -1)
    if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
        return false;  // Not a valid key return true;
    return true; }
}
 function TrimText(txt)
function TrimText(txt) {
{ return txt.replace(/^\s*|\s*$/g,"");
    return txt.replace(/^\s*|\s*$/g,""); }
}
 function FormatNumber(nStr)
function FormatNumber(nStr) {
{ //add commas to number
    //add commas to number nStr += '';
    nStr += ''; x = nStr.split('.');
    x = nStr.split('.'); x1 = x[0];
    x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : '';
    x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/;
    var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) {
    while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2');
        x1 = x1.replace(rgx, '$1' + ',' + '$2'); }
    } return x1 + x2;
    return x1 + x2; }
}
 function RemoveCommas(str)
function RemoveCommas(str) {
{ return str.toString().replace(new RegExp(/,/g), "");
    return str.toString().replace(new RegExp(/,/g), ""); }
}
 function IsEmptyString(str)
function IsEmptyString(str) {
{ if (TrimText(str).length == 0)
    if (TrimText(str).length == 0) return true;
        return true; else
    else return false;
        return false; }
}
 function IsEmailValid(str)
function IsEmailValid(str)  {
{ 
         var at="@"
        var at="@" var dot="."
        var dot="." var lat=str.indexOf(at)
        var lat=str.indexOf(at) var lstr=str.length
        var lstr=str.length var ldot=str.indexOf(dot)
        var ldot=str.indexOf(dot) if (str.indexOf(at)==-1){
        if (str.indexOf(at)==-1){ return false
          return false }
        }
 if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false
          return false }
        }
 if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false
           return false }
        }
 if (str.indexOf(at,(lat+1))!=-1){
         if (str.indexOf(at,(lat+1))!=-1){ return false
            return false }
         }
 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false
            return false }
         }
 if (str.indexOf(dot,(lat+2))==-1){
         if (str.indexOf(dot,(lat+2))==-1){ return false
            return false }
         } 
         if (str.indexOf(" ")!=-1){
         if (str.indexOf(" ")!=-1){ return false
            return false }
         }
 return true
          return true                     }
    }
 function ValidateNonNumericField(fld, e, allowSpace, isDouble, errorMessage)
function ValidateNonNumericField(fld, e, allowSpace, isDouble, errorMessage) {
{ var space='';
    var space=''; var strCheck;
    var strCheck; if(allowSpace!=null && allowSpace==true)
    if(allowSpace!=null && allowSpace==true) space=' ';
        space=' '; if (isDouble !=null && isDouble == true)
    if (isDouble !=null && isDouble == true) strCheck = '0123456789.' + space;
     strCheck = '0123456789.' + space; else
    else strCheck = '0123456789' + space;
     strCheck = '0123456789' + space; 
      for (i = 0; i < fld.value.length; i++)
    for (i = 0; i < fld.value.length; i++) {
    { key = fld.value.charAt(i);  // Get key value from key code
        key = fld.value.charAt(i);  // Get key value from key code if (strCheck.indexOf(key) == -1)
        if (strCheck.indexOf(key) == -1) {
        { alert(errorMessage);
            alert(errorMessage); fld.value = '';
            fld.value = ''; fld.focus();
            fld.focus(); return;
            return; }
        } }
    } }
}
 function ValidateSpecialCharactersField(fld,e,allowedChars,spaceAllowed,numAllowed,errorMessage)
function ValidateSpecialCharactersField(fld,e,allowedChars,spaceAllowed,numAllowed,errorMessage) {
{ var space=' ';
    var space=' '; //by default space is allowed
    //by default space is allowed if(spaceAllowed!=null && spaceAllowed==false)
    if(spaceAllowed!=null && spaceAllowed==false) space='';
        space=''; //by default numbers r not allowed
    //by default numbers r not allowed var num='';
    var num=''; if(numAllowed!=null && numAllowed==true)
    if(numAllowed!=null && numAllowed==true) num='0123456789';
        num='0123456789'; 
         var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;
    var strCheck = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'+space+allowedChars+num;
 for (i = 0; i < fld.value.length; i++)
    for (i = 0; i < fld.value.length; i++) {
    { key = fld.value.charAt(i);  // Get key value from key code
        key = fld.value.charAt(i);  // Get key value from key code if (strCheck.indexOf(key) == -1)
        if (strCheck.indexOf(key) == -1) {
        { alert(errorMessage);
            alert(errorMessage); fld.value = '';
            fld.value = ''; fld.focus();
            fld.focus(); return;
            return; }
        } }
    } }
}
 // var test = Round(15.1356, 2);
// var test = Round(15.1356, 2); // Result: test = 15.14
// Result: test = 15.14 function Round(a_Num , a_Bit)
function Round(a_Num , a_Bit) {
{ return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit));
    return(Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit)); }
}  
                    
                

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号