C# 校验填写金额是否正确的方法
C#校验填写金额是否正确的方法
using System.Text.RegularExpressions;
namespace UnifyPayPlatAPI.Controller
{
public class AmountsCheck
{
public bool Check(string str)
{
bool _bool;
// 使用正则表达式进行匹配,金额必须只包含数字和小数点
_bool = Regex.IsMatch(str, @"^[0-9.]+$");
if (_bool) { }
else
{
return _bool;
}
//判断前置小数点
var dotleft = str.IndexOf('.');
var _dotleft = str.LastIndexOf('.');
if (dotleft == 0 || dotleft != _dotleft || (dotleft != -1 && dotleft == (str.Length - 1)))
{
_bool = false;
return _bool;
}
_bool = true;
return _bool;
}
}
}以上方法返回值为bool类型,为true则格式正确,具体功能如下:
1、判断填写内容是否仅为数字和小数点;
2、判断小数点是否在首位;
3、判断小数点是否在末尾;
4、判断是否存在多个小数点;

