在textbox中输入字符串,字符可以为中文、英文或各种字符,使用控件验证,如何用正则表达式判断字符串长度,比如字符串长度小于等于10个?
另外一个汉字和一个英文字符是否都占用相同的位置?也就是说,限制10个字符,可以输入10个英文字母,是否可以输入10个汉字?谢谢。

解决方案 »

  1.   

    汉字占2个字节
    应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;} 
      

  2.   


    LZ的表达能力啊
    if(this.value.length>10)alert('fuck you,Your input 太XX many .)
      

  3.   

    String.prototype.len=function(){return this.replace([^x00-xff]/g,"").length;}public static int GetLength(string strSource)
            {
                Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
                int nLength = strSource.Length;
                for(int i=0; i<strSource.Length; i++)
                {
                    if (regex.IsMatch(strSource.Substring(i,1))) 
                    {
                        nLength++;
                    }
                }
                return nLength;
            }
      

  4.   

    请用设置TEXTBOX内MAXlength属性
    控制最大输入长度。
    这样的话,输入的汉字只占1个单位,和英文,符号所占单位相同