[Asp.Net]判断输入文字是否是数字
2009-04-28 09:01
881 查看
asp.net判断输入文字是否是数字
方案一:
/**//// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
public bool IsNumberic(string oText)
{
try
{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
{
return false;
}
}
try catch方法
例:
try
{
Convert.ToInt32("123"):
Console.Write("是数字");
}
catch(Exception ex)
{
Console.Write("非数字");
}
注:如果有很多字符串要求判断,此方法需要大量的try catch 以及finally来处理后续的程序.不建议使用此方法。
改进一下:
因为可以转int 可以转Decimal
public bool IsNumberic(string oText)
{
try
{
Decimal Number = Convert.ToDecimal (oText);
return true;
}
catch
{
return false;
}
}
方案二:
//如果是纯数字还可以采用ASCII码进行判断
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="str">字符串</param>
/// <returns>bool</returns>
public bool IsNumeric(string str)
{
if (str == null || str.Length == 0)
return false;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytestr = ascii.GetBytes(str);
foreach (byte c in bytestr)
{
if (c < 48 || c > 57)
{
return false;
}
}
return true;
}
方案三:
正则表达式方法
例:
//引用正则表达式类
using System.Text.RegularExpressions;
Regex reg=new Regex("^[0-9]+$");
Match ma=reg.Match(text);
if(ma.Success)
{
//是数字
}
else
{
//不是数字
}
注:此方法快捷,但不太容易掌握,尤其是正则表达式公式,如果有兴趣的朋友可以好好研究,这东西很好用的,建议使用。
方案四:
Double.TryParse方法
例:
bool isNum=System.Double.TryParse("所要判断的字符串" ,System.Globalization.NumberStyles.Integer,null,out );
注:此方法快捷,方便,很容易被掌握,但是参数很多,有兴趣的朋友可以研究一下,建议使用。
参数不好用
没有使用过
方法五:
新建一个类
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace LBC.Number
{
/// <summary>
/// 数字判断的类
/// </summary>
public class NumberClass
{
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="strNumber">要判断的字符串</param>
/// <returns></returns>
public static bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+___FCKpd___0quot;;
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*___FCKpd___0quot;;
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
/// <summary>
/// 判断是否是int类型
/// </summary>
/// <param name="Value">要判断的字符串</param>
/// <returns></returns>
public static bool IsInt(string Value)
{
return Regex.IsMatch(Value, @"^[+-]?/d*___FCKpd___0quot;);
}
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="Value">要判断的字符串</param>
/// <returns></returns>
public static bool IsNumeric(string Value)
{
return Regex.IsMatch(Value, @"^[+-]?/d*[.]?/d*___FCKpd___0quot;);
}
}
}
方案一:
/**//// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
public bool IsNumberic(string oText)
{
try
{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
{
return false;
}
}
try catch方法
例:
try
{
Convert.ToInt32("123"):
Console.Write("是数字");
}
catch(Exception ex)
{
Console.Write("非数字");
}
注:如果有很多字符串要求判断,此方法需要大量的try catch 以及finally来处理后续的程序.不建议使用此方法。
改进一下:
因为可以转int 可以转Decimal
public bool IsNumberic(string oText)
{
try
{
Decimal Number = Convert.ToDecimal (oText);
return true;
}
catch
{
return false;
}
}
方案二:
//如果是纯数字还可以采用ASCII码进行判断
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="str">字符串</param>
/// <returns>bool</returns>
public bool IsNumeric(string str)
{
if (str == null || str.Length == 0)
return false;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytestr = ascii.GetBytes(str);
foreach (byte c in bytestr)
{
if (c < 48 || c > 57)
{
return false;
}
}
return true;
}
方案三:
正则表达式方法
例:
//引用正则表达式类
using System.Text.RegularExpressions;
Regex reg=new Regex("^[0-9]+$");
Match ma=reg.Match(text);
if(ma.Success)
{
//是数字
}
else
{
//不是数字
}
注:此方法快捷,但不太容易掌握,尤其是正则表达式公式,如果有兴趣的朋友可以好好研究,这东西很好用的,建议使用。
方案四:
Double.TryParse方法
例:
bool isNum=System.Double.TryParse("所要判断的字符串" ,System.Globalization.NumberStyles.Integer,null,out );
注:此方法快捷,方便,很容易被掌握,但是参数很多,有兴趣的朋友可以研究一下,建议使用。
参数不好用
没有使用过
方法五:
新建一个类
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace LBC.Number
{
/// <summary>
/// 数字判断的类
/// </summary>
public class NumberClass
{
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="strNumber">要判断的字符串</param>
/// <returns></returns>
public static bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+___FCKpd___0quot;;
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*___FCKpd___0quot;;
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
/// <summary>
/// 判断是否是int类型
/// </summary>
/// <param name="Value">要判断的字符串</param>
/// <returns></returns>
public static bool IsInt(string Value)
{
return Regex.IsMatch(Value, @"^[+-]?/d*___FCKpd___0quot;);
}
/// <summary>
/// 判断是否是数字
/// </summary>
/// <param name="Value">要判断的字符串</param>
/// <returns></returns>
public static bool IsNumeric(string Value)
{
return Regex.IsMatch(Value, @"^[+-]?/d*[.]?/d*___FCKpd___0quot;);
}
}
}
相关文章推荐
- asp.net判断输入文字是否是数字
- ASP.NET判断输入的是否为数字
- ASP.NET判断输入的是否为数字
- 判断输入的是否为数字 C#, ASP.NET
- ASp.net 文本框(TextBox)计算,判断输入的是否是数字
- ASp.net的文本框(TextBox)计算,判断输入的是否是数字
- Asp.net 判断输入的字符串是不是数字
- Asp.net 判断输入的字符串是不是数字
- 在c#.net中,如何判断文本框中的值是否是数字?我想验证身份证输入是否正确。
- asp.net 判断字符串是否是数字的,int的,整型的
- asp.net中判断是否为数字
- asp.net中利用正则表达式判断一个字符串是否为数字的代码
- C#/asp.net 正则表达式 判断邮件格式- -判断是否为数字
- Asp.net 判断输入的字符串是不是数字
- asp.net 判断是否为数字
- vb asp.net 判断值是否为数字
- 判断在VB.NET中输入一个数字是否是一个数字?
- 每日总结(二)C#判断输入内容为数字、Sharepoint回收应用程序池错误、Asp.Net 会发或回调参数无效、DropDownList动态绑定且设置默认选择项
- c#判断输入文字是否是数字