一些简单的验证(中文验证、数字验证)

^[\u4e00-\u9fa5]{1,20}$ 这个是 验证中文的正则表达式

System.Text.Encoding.Default.GetByteCount( strString );--返回字节数


//判断字符串是否是数字
 string str1=this.textBox1.Text.Trim();
 if(Regex.IsMatch(str1,@"^\d+$"))
 {
  MessageBox.Show("是数字.");//是数字;
 }
 else
  MessageBox.Show("不是数字");
return;
 //

 //using System.Text.RegularExpressions;
 string str="23sd呜SDF呜95列gfwef随地56国GE嘎4";
 MatchCollection ms=Regex.Matches(str,"[\u4e00-\u9fa5]");
 string sss=null;
 for(int i=0;i<ms.Count;i++)
 {
  sss+=ms[i].Value;
 }
 MessageBox.Show(sss);

 if (Regex.IsMatch(str,"[0-9]"))//数字
  MessageBox.Show("字符串中包含有数字");
if (Regex.IsMatch(str,"[a-z]"))//小写字母
  MessageBox.Show("字符串中包含有小写字母");
 if (Regex.IsMatch(str,"[A-Z]"))//大写字母
  MessageBox.Show("字符串中包含有小写字母");
 if (Regex.IsMatch(str,"[a-zA-Z]"))//所有字母
  MessageBox.Show("字符串中包含有字母");
if (Regex.IsMatch(str,"[\u4e00-\u9fa5]"))//汉字
  MessageBox.Show("字符串中包含有汉字");


}

 


if (IsLength(TextBox.text,100) == false) //超过100字

public bool IsLength(string str, int maxLength)
{
 char c;
 int intLength = 0;

 for (int i = 0; i < str.Length; i++)
 {
  c = str[i];
 
  if ((c < 128) || ((c > 160) && (c < 224)) || ((c > 65376) && (c < 65439)))
  {
   intLength ++;
  }
  else
  {
   intLength += 2;
  }
 }

 if (intLength > maxLength)
 {
  return false;
 }

 return true;
}

 

posted @ 2005-09-03 18:27  浪剑  阅读(2627)  评论(4编辑  收藏  举报