using System.Text.RegularExpressions;
#region 驗證輸入的字符串
/// <summary>
/// 判斷輸入的字符類型
/// </summary>
/// <param name="_value">輸入的字串</param>
/// <param name="_kind">要驗證的類型</param>
/// 1: 由26個英文字母組成的字串
/// 2: 正整數
/// 3: 非負整數(正整數 + 0)
/// 4: 非正整數(負整數 + 0)
/// 5: 負整數
/// 6: 整數
/// 7: 非負浮點數(正浮點數 + 0)
/// 8: 正浮點數
/// 9: 非正浮點數(負浮點數 + 0)
/// 10: 負浮點數
/// 11: 浮點數
/// 12: 由26個英文字母的大寫組成的字串
/// 13: 由26個英文字母的小寫組成的字串
/// 14: 由數位和26個英文字母組成的字串
/// 15: 由數位、26個英文字母或者下劃線組成的字串
/// 16: Email
/// 17: URL
/// 18: 只能輸入入中文
/// 19:
/// </param>
/// <returns>true是驗証通過,false是驗証錯誤</returns>
/// <returns></returns>
public bool ValidateUserInput(String _value, int _kind)
{
string RegularExpressions = null;
switch (_kind)
{
case 1:
//由26個英文字母組成的字串
RegularExpressions = "^[A-Za-z]+$";
break;
case 2:
//正整數
RegularExpressions = "^[0-9]*[1-9][0-9]*$";
break;
case 3:
//非負整數(正整數 + 0)
RegularExpressions = "^\\d+$";
break;
case 4:
//非正整數(負整數 + 0)
RegularExpressions = "^((-\\d+)|(0+))$";
break;
case 5:
//負整數
RegularExpressions = "^-[0-9]*[1-9][0-9]*$";
break;
case 6:
//整數
RegularExpressions = "^-?\\d+$";
break;
case 7:
//非負浮點數(正浮點數 + 0)
RegularExpressions = "^\\d+(\\.\\d+)?$";
break;
case 8:
//正浮點數
RegularExpressions = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
break;
case 9:
//非正浮點數(負浮點數 + 0)
RegularExpressions = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
break;
case 10:
//負浮點數
RegularExpressions = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
break;
case 11:
//浮點數
RegularExpressions = "^(-?\\d+)(\\.\\d+)?$";
break;
case 12:
//由26個英文字母的大寫組成的字串
RegularExpressions = "^[A-Z]+$";
break;
case 13:
//由26個英文字母的小寫組成的字串
RegularExpressions = "^[a-z]+$";
break;
case 14:
//由數位和26個英文字母組成的字串
RegularExpressions = "^[A-Za-z0-9]+$";
break;
case 15:
//由數位、26個英文字母或者下劃線組成的字串
RegularExpressions = "^[0-9a-zA-Z_]+$";
break;
case 16:
//email地址
RegularExpressions = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
break;
case 17:
//url
RegularExpressions = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
break;
case 18:
//只能輸入中文
RegularExpressions = "^[^\u4E00-\u9FA5]";
break;
case 19:
//只能输入0和非0打头的数字
RegularExpressions = "^(0|[1-9][0-9]*)$";
break;
case 20:
//只能输入数字
RegularExpressions = "^[0-9]*$";
break;
case 21:
//只能輸入數字加2位小數
RegularExpressions = "^[0-9]+(.[0-9]{1,2})?$";
break;
case 22:
//只能输入0和非0打头的数字加2位小數
RegularExpressions = "^(0|[1-9]+)(.[0-9]{1,2})?$";
break;
case 23:
//只能输入0和非0打头的数字加2位小數 但不匹配0.00
RegularExpressions = "^(0(.(0[1-9]|[1-9][0-9]))?|[1-9]+(.[0-9]{1,2})?)$";
break;
default:
break;
}
Match m = Regex.Match(_value, RegularExpressions);
if (m.Success)
return true;
else
return false;
}
#endregion