字符串长度校验

JS 方法:

 

function fucCheckLength(strTemp)    
{    
    var i,sum;    
    sum=0;    
    for(i=0;i<strTemp.length;i++)    
    {    
         if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255))    
             sum=sum+1;    
         else   
            sum=sum+2;    
     }    
     return sum;    
}    
 

 

.Net 服务端

public bool CheckLen(string str, int intlen)
{
	bool blnResult = false;
	int n = 0;
	int intLenAct = 0;

	for (n = 1; n <= Strings.Len(str); n++) {
		if (Strings.AscW(Strings.Mid(str, n, 1)) > 256) {
			intLenAct = intlen + 2;
		} else {
			intLenAct = intlen + 1;
		}
	}

	if (intLenAct <= intlen) {
		blnResult = true;
	}

	return blnResult;

}

posted @ 2011-01-14 00:04  fangxianjuner  阅读(445)  评论(0编辑  收藏  举报