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));
}
{
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));
}