C#户在文本框中输入一个字符,判断输入的是属于什么字符(字母(a-z,A-Z)、数字(0-9)

如题所述

第1个回答  2011-09-30
/// <summary>
/// 功能: 判断是否输入的是 数字或字母
/// 输入参数: 要判断的字符串
/// 返回: 大于0 符合条件 ,小于等于0 不符合条件
/// </summary>
public static int strValidate(string strValue)
{
string strCriterion = @"^[0-9a-zA-Z]*$";
int iSuccess = 0;
Match m = Regex.Match(strValue, strCriterion); // 匹配正则表达式
if (m.Success) // 输入的是数字或字母
{
iSuccess = 1; // 内容不变
}
return iSuccess;
}

/// <summary>
/// 功能: 判断是否输入的是数字
/// 输入参数: 要判断的字符串
/// 返回: 大于0 符合条件 ,小于等于0 不符合条件
/// </summary>
public static int strValidateLetter(string strValue)
{
string strCriterion = @"^[0-9]*$";
int iSuccess = 0;
Match m = Regex.Match(strValue, strCriterion); // 匹配正则表达式
if (m.Success) // 输入的是数字
{
iSuccess = 1;
}

return iSuccess;
}
//1-9]{1}[0-9]{0,2}$
/// <summary>
/// 邮箱格式的验证
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static string strValidateEmail(string strValue,ref string strErrorMessage)
{
//\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
string strCriterion = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
string Criterion = "";
Match m = Regex.Match(strValue, strCriterion); // 匹配正则表达式
if (!m.Success) // 输入的邮箱格式不正确
{
strValue = Criterion;
strErrorMessage = "输入的邮箱格式不正确";
}
else // 输入的邮箱格式正确
{
Criterion = strValue; // 输入的值保存下来
}
return strValue;
}追问

我把问题详细的说下 刚刚没写全 需要你的再次帮助 谢谢了!
用户在文本框中输入一个字符,判断输入的是属于什么字符(字母(a-z,A-Z)、数字(0-9)、其它),并在标签框中输出以下三个值的一个:是字母、是数字、是其它字符。 必须是用Visual C# 2008 编写的程序代码 及求解

第2个回答  2011-09-30
public static void Main()
{
var str = "123,abc XYZ";

for (int i=0;i<str.Length;i++){
if (char.IsLower(str[i]))
Console.WriteLine("{0} 是小写字母", str[i]);

if (char.IsPunctuation(str[i]))
Console.WriteLine("{0} 是标点符号", str[i]);

if (char.IsDigit(str[i]))
Console.WriteLine("{0} 是数字",str[i]);

if (char.IsWhiteSpace(str[i]))
Console.WriteLine("{0} 是空格或空白字符", str[i]);
if(char.IsUpper(str[i]))
Console.WriteLine("{0} 是大写字母", str[i]);

}
Console.ReadKey();
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网