一、获取字符串的实际长度:
function getStrActualLen(sChars)
{
return sChars.replace(/[^\x00-\xff]/g,"xx").length;
}
----------------------推荐上面的方法---------------------------------
function getTrueLenth(str)
{
if(typeof str != 'string')
{
str = new String(str);
}
var TrueLength = 0;
for ( var i=0;i<str.length;i++)
{
if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94)
{
alert("1");
TrueLength += 2;
}
else
{
alert("2");
TrueLength += 1;
}
}
return TrueLength;
}
其他一些方法,还没来得及整理
//页面里回车到下一控件的焦点
function Enter2Tab(e)
{
try
{
var ob = IsFireFox ? e.target : event.srcElement;
if(ob.tagName == "INPUT" &&
(
ob.type == "text" ||
ob.type == "password" ||
ob.type == "checkbox" ||
ob.type == "radio"
) ||
ob.tagName == "SELECT")
{
var key = IsFireFox ? e.which : event.keyCode;
if (key == 13)
{
if (IsFireFox)
{
event.which = 9;
}
else
{
event.keyCode = 9;
}
}
}
}
catch(E){}
}
//打开此功能请取消下行注释
//document.onkeydown = Enter2Tab;
//ajax对象创建
function AjaxLib()
{
try
{
if (typeof ActiveXObject != "undefined")
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
}
catch(a){}
return null;
}
//判断用户名是否符合标准
function UserNameIsOk(ssn)
{
var re = /^[0-9a-z][\w-.]*[0-9a-z]$/i;
return re.test(ssn) ? true : false;
}
function SelectAll()
//_______全选择__________
{
var empty;
var f = document.forms[0];
for (var i = 0; i < f.length; i++)
{
empty = f;
if (empty.type == "checkbox" && empty.disabled == false)
empty.checked = true;
}
}
function SelectReverse()
//__________返选择_________
{
var empty;
var f = document.forms[0];
for (var i = 0; i < f.length; i++)
{
empty = f;
if (empty.type == "checkbox" && empty.disabled == false)
if(empty.checked == true)
{
empty.checked = false;
}
else
{
empty.checked = true;
}
}
}
//提交查询内容 submit search find content
function GoPage(pn)
{
try
{
document.forms[0].page.value = pn;
document.forms[0].submit();
}
catch(E)
{
document.forms[1].page.value = pn;
document.forms[1].submit();
}
}
--------------------------------------------------------------------------------
function WoodsAlert(ob, tip)
{
alert(tip);
//ob.value = "";
ob.focus();
}
//__________处理特殊字符________________
function invalid_keyword()
{
var keyword = new Array("<", ">", "\%", "\'", "\"");
var f, ob;
for (var form = 0; form < document.forms.length; form++)
{
f = document.forms[form];
for (var i = 0; i < f.length; i++)
{
ob = f;
if (ob.type == "text" && ob.disabled == false)
{
for (j = 0; j < keyword.length; j++)
{
if (ob.value.indexOf(keyword[j]) != -1)
{
alert("在输入框聚焦处发现非法字符[" + keyword[j] + "]。");
//ob.value = "";
ob.focus();
return false;
}
}
ob.value = Trim(ob.value);
}
}
}
return true;
}
function IsTime(str)
//__________判断是否为时间字符串________________________
{
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) {alert('输入的参数不是时间格式'); return false;}
if (a[1]>24 || a[3]>60 || a[4]>60)
{
return false
}
return true;
}
function IsDate(str)
//_________是否为日期(YYYY-MM-DD)类型字符串_________________________
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}
function IsBigCurrentDate(str)
//_________________判断用户给出的日期是否大于当前日期______________________________
{
var today = new Date();
var tY = String(today.getFullYear());
var tM = String(today.getMonth() + 1);
(tM.length <= 1) ? tM = "0" + tM : tM = tM;
var tD = String(today.getDate());
(tD.length <= 1) ? tD = "0" + tD : tD = tD;
var tH = String(today.getHours());
(tH.length <= 1) ? tH = "0" + tH : tH = tH;
var tM1 = String(today.getMinutes());
(tM1.length <= 1) ? tM1 = "0" + tM1 : tM1 = tM1;
var tdate = tY + "-" + tM + "-" + tD + " " + tH + ":" + tM1 + ":59";//zhuweisong 添加秒的叛断
if (str > tdate)
{
return true;
}
else
{
return false;
}
}
function IsDateTime(str)
//____________是否为时间日期类型__________________________________
{
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if (r == null) return false;
var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}
function IsEmail(mail)
//_____________判断是否是有效的EMAIL地址________________________________
{
return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));
}
function IsIp(s)
//________________判断是否为有效IP地址_________________________________
{
var check = function(v)
{
try
{
return (v <= 255 && v >= 0);
}
catch(x)
{
return false;
}
}
var re = s.split(".")
return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false
}
//_________判断输入的是否为汉字___________begin__________
function isCharsInBag (s, bag)
{
var i,c;
for (i = 0; i < s.length; i++)
{
c = s.charAt(i);//字符串s中的字符
if (bag.indexOf(c) > -1)
return c;
}
return "";
}
function IsCn(s)
{
var errorChar;
var badChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789><,[]{}?/+=|\'\":;~!#$%()`";
errorChar = isCharsInBag(s, badChar)
if (errorChar != "" )
{
return false;
}
return true;
}
//_________判断输入的是否为汉字___________end__________
//________函数功能:判断输入的字符串是不是英文__________begin___________
function isCharsInBagEn (s, bag)
{
var i,c;
for (i = 0; i < s.length; i++)
{
c = s.charAt(i);//字符串s中的字符
if (bag.indexOf(c) <0)
return c;
}
return "";
}
function IsEn(s)
{
var errorChar;
var badChar = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
errorChar = isCharsInBagEn(s, badChar)
return (errorChar != "" ) ? false : true;
}
//________函数功能:判断输入的字符串是不是英文__________end___________
/********************
函数名称:IsTelephone
函数功能:固话,手机号码检查函数,合法返回true,反之,返回false
函数参数:obj,待检查的号码
检查规则:
(1)电话号码由数字、"("、")"和"-"构成
(2)电话号码为3到8位
(3)如果电话号码中包含有区号,那么区号为三位或四位
(4)区号用"("、")"或"-"和其他部分隔开
(5)移动电话号码为11或12位,如果为12位,那么第一位为0
(6)11位移动电话号码的第一位和第二位为"13"
(7)12位移动电话号码的第二位和第三位为"13"
********************/
function IsTelephone(obj)
{
var pattern=/(^([0\+]\d{2,3})\d{3,4}\-\d{3,8}$)|(^([0\+]\d{2,3})\d{3,4}\d{3,8}$)|(^([0\+]\d{2,3}){0,1}13\d{9}$)|(^\d{3,4}\d{3,8}$)|(^\d{3,4}\-\d{3,8}$)/;
return (pattern.test(obj)) ? true : false;
}
/********************
函数名称:IsLegality
函数功能:检查字符串的合法性,即是否包含" '字符,包含则返回false;反之返回true
函数参数:obj,需要检测的字符串
********************/
function IsLegality(obj)
{
var intCount1 = obj.indexOf("\"",0);
var intCount2 = obj.indexOf("\'",0);
return (intCount1 > 0 || intCount2 > 0) ? false : true;
}
/********************
函数名称:IsNumber
函数功能:检测字符串是否全为数字
函数参数:str,需要检测的字符串
********************/
function IsNumber(str)
{
var number_chars = "1234567890";
var i;
for (i = 0; i < str.length; i++)
{
if (number_chars.indexOf(str.charAt(i))==-1) return false;
}
return true;
}
/********************
函数名称:Trim
函数功能:去除字符串两边的空格
函数参数:str,需要处理的字符串
********************/
function Trim(str)
{
return str.replace(/(^\s*)|(\s*$)/g, "");
}
/*-------------------------------------------------------------------------------------------------------*/
/********************
函数名称:LTrim
函数功能:去除左边的空格
函数参数:str,需要处理的字符串
********************/
function LTrim(str)
{
return str.replace(/(^\s*)/g, "");
}
[/code]