第1个回答 2010-12-31
1.
public bool textValueIsInt(object obj)
{
bool boolFlag=true;
try
{
Convert.ToInt32(obj);
}
catch()
{
boolFlag=false;
}
return boolFlag;
}
2
使用正则:
/// <summary>
/// 检测是否整数型数据
/// </summary>
/// <param name="Num">待检查数据</param>
/// <returns></returns>
public static bool IsInteger(string Input)
{
if (Input == null)
{
return false;
}
else
{
return IsInteger(Input, true);
}
}
/// <summary>
/// 是否全是正整数
/// </summary>
/// <param name="Input"></param>
/// <returns></returns>
public static bool IsInteger(string Input, bool Plus)
{
if (Input == null)
{
return false;
}
else
{
string pattern = "^-?[0-9]+$";
if (Plus)
pattern = "^[0-9]+$";
if (Regex.Match(Input, pattern, RegexOptions.Compiled).Success)
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 判断输入是否为日期类型
/// </summary>
/// <param name="s">待检查数据</param>
/// <returns></returns>
public static bool IsDate(string s)
{
try
{
DateTime d = DateTime.Parse(s);
return true;
}
catch
{
return false;
}
}
第2个回答 2010-12-31
输入的都是字符串
只能根据自己的需要转换成对应的类型
可以使用Convert类来进行转换,并通过捕获异常还判断输入是否正确本回答被网友采纳
第3个回答 2010-12-31
可以用最强大的正则验证啊!如下面验证输入的用户名和密码:
Regex rx = new Regex("(^[\u4e00-\u9fa5]{2,6}$)|(^[a-zA-Z0-9]{4,12}$)|(^[\u4e00-\u9fa5a-zA-Z0-9]{4,10}$)");
Match ma1 = rx.Match(strUserName);
Match ma2 = rx.Match(strPassword);
bool bo1 = ma1.Success;
bool bo2 = ma2.Success;
if (bo1 == true && bo2 == true)
{
return true;
}
else
{
return false;
}
第4个回答 2010-12-31
哪用得着那么麻烦,
Console.WriteLine("变量名".GetType().ToString());
输入的就是当前变量的类型