用正则表达式也可以 static bool IsNumeric(string str) { if (str==null || str.Length==0) return false; foreach(char c in str) { if (!Char.IsNumber(c)) { return false; } } return true; } 正则表达的写法是: static bool IsNumeric(string str) { System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$"); return reg1.IsMatch(str); }
使用证则表达式验证数字: using System.Text.RegularExpressions; using System;
class Validation { public static void Main() { string strToTest; Validation objValidate=new Validation();
Console.Write("Enter a String to Test for Alphabets:"); strToTest=Console.ReadLine(); //Change here to whatever validation you want to perform if(objValidate.IsAlpha(strToTest)) { Console.WriteLine("{0} is Valid Alpha String",strToTest); } else { Console.WriteLine("{0} is not a Valid Alpha String",strToTest); } }
// Function to test whether the string is valid number or not public 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]+$"; String strValidIntegerPattern="^([-]|[0-9])[0-9]*$"; Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
static bool IsNumeric(string str)
{
if (str==null || str.Length==0)
return false;
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
} 正则表达的写法是:
static bool IsNumeric(string str)
{
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
return reg1.IsMatch(str);
}
using System.Text.RegularExpressions;
using System;
class Validation
{
public static void Main()
{
string strToTest;
Validation objValidate=new Validation();
Console.Write("Enter a String to Test for Alphabets:");
strToTest=Console.ReadLine();
//Change here to whatever validation you want to perform
if(objValidate.IsAlpha(strToTest))
{
Console.WriteLine("{0} is Valid Alpha String",strToTest);
}
else
{
Console.WriteLine("{0} is not a Valid Alpha String",strToTest);
}
}
// Function to test for Positive Integers.
public bool IsNaturalNumber(String strNumber)
{
Regex objNotNaturalPattern=new Regex("[^0-9]");
Regex objNaturalPattern=new Regex("0*[1-9][0-9]*");
return !objNotNaturalPattern.IsMatch(strNumber) &&
objNaturalPattern.IsMatch(strNumber);
}
// Function to test for Positive Integers with zero inclusive
public bool IsWholeNumber(string strNumber)
{
Regex objNotWholePattern=new Regex("[^0-9]");
return !objNotWholePattern.IsMatch(strNumber);
}
// Function to Test for Integers both Positive & Negative
public bool IsInteger(string strNumber)
{
Regex objNotIntPattern=new Regex("[^0-9-]");
Regex objIntPattern=new Regex("^-[0-9]+$|^[0-9]+$");
return !objNotIntPattern.IsMatch(strNumber) &&
objIntPattern.IsMatch(strNumber);
}
// Function to Test for Positive Number both Integer & Real
public bool IsPositiveNumber(string strNumber)
{
Regex objNotPositivePattern=new Regex("[^0-9.]");
Regex objPositivePattern=new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
return !objNotPositivePattern.IsMatch(strNumber) &&
objPositivePattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber);
}
// Function to test whether the string is valid number or not
public 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]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
// Function To test for Alphabets.
public bool IsAlpha(string strToCheck)
{
Regex objAlphaPattern=new Regex("[^a-zA-Z]");
return !objAlphaPattern.IsMatch(strToCheck);
}
// Function to Check for AlphaNumeric.
public bool IsAlphaNumeric(string strToCheck)
{
Regex objAlphaNumericPattern=new Regex("[^a-zA-Z0-9]");
return !objAlphaNumericPattern.IsMatch(strToCheck);
}
}
try
{
orderid = System.Convert.ToInt32(Request.QueryString["orderid"].Trim());
//转换成功则继续执行。
}
catch (Exception ex)
{
//错误处理
}
方法二:
using System.Text.RegularExpressions;
string ex = "^[0-9]*$";
Regex reg = new Regex( ex,RegexOptions.IgnoreCase );
bool flag = reg.IsMatch( 要验证的string );
方法三:
public bool isNum()
{
string B=textBox1.Text;
try
{
double b=Convert.ToDouble(B);
return true;
}
catch(Exception)
{
return false;
}
}方法四:
public bool IsNum(string s)
{
foreach(char c in textBox1.Text)
{
if(!Char.IsDigit(c))
return false;
}
return true;
}
//此方法中能判断为整型,不能判断浮点型。
public bool IsInt(string InputStr)
{
//String Reg = @"^[0-9]*$";//是否是正整数>=0
String Reg = @"^-?\d+$"; //是否是整数,含正负整数0
if(Regex.IsMatch(InputStr.Trim(), Reg))
{
return true;
}
else
{
return false;
}
}//是否是浮点数,含正负0public bool IsNum(string InputStr)
{
//String Reg = @"^\d+(\.\d+)?$"; //是否是正浮点数,
String Reg = @"^(-?\d+)(\.\d+)?$"; //是否是浮点数,含正负0
if(Regex.IsMatch(InputStr.Trim(), Reg))
{
return true;
}
else
{
return false;
}
}
/// 判断字符是否为数字
/// </summary>
/// <param name="_strNum">传入的字符串</param>
/// <returns>boolean</returns>
public static bool isNumeric(string _strNum)
{
if (_strNum == null || _strNum == string.Empty)
return false;
for (int i = 0; i < _strNum.Length; i++)
{
if (!Char.IsNumber(_strNum, i))
return false;
}
return true;
}
char.IsNumeric()
就可以搞定了 这里犯不着用正则吧~~~