/// <summary> /// 注册验证方法 /// </summary> /// <param name="RegularType">服务提供的验证方式</param> /// <param name="InvalidatedInfo">验证无效时的提示信息</param> public void ResigerValidateMethod(EnumRegularType RegularType, string InvalidatedInfo) { this._invalidatedInfo = InvalidatedInfo; this._regularType = RegularType; }
/// <summary> /// 注册验证方法 /// </summary> /// <param name="CustomRegularString">自定义的正则表达式,如果该表达式为空,则调用事件委托OnValidate进行验证。</param> /// <param name="InvalidatedInfo">验证无效时的提示信息</param> public void ResigerValidateMethod(string CustomRegularString, string InvalidatedInfo) { this._invalidatedInfo = InvalidatedInfo; this._regularType = EnumRegularType.Custom; this._customRegularString = CustomRegularString; }
/// <summary> /// 通过正则表达式判断输入是否正确 /// </summary> /// <param name="enumRegularType">试用正则表达式的类型</param> /// <param name="strInput">需要验证的字符串</param> /// <returns>结果</returns> public static bool RegularData(EnumRegularType enumRegularType, string strInput) { switch (enumRegularType) { case EnumRegularType.Int: { Regex check = new Regex(@"^[0-9]*[1-9][0-9]*$"); return(check.IsMatch(strInput)); }; case EnumRegularType.String: { if (strInput == string.Empty) { return(false); } else { return(true); } }; case EnumRegularType.Email: { Regex check = new Regex(@"(?<email>(\w|\d|-|_)+@([^\. \@]+\.)+[^\. \@]+)"); return(check.IsMatch(strInput)); }; case EnumRegularType.Mobile: { Regex check = new Regex(@"^((\+86)|(86))?(1)\d{10}$"); return(check.IsMatch(strInput)); }; case EnumRegularType.Phone: { Regex check = new Regex(@"((\(\d{3}\)|(\d{4})|\d{3}-|\d{4}-)(\d{7}|\d{12}))|(\d{7}|\d{12})"); return(check.IsMatch(strInput)); }; case EnumRegularType.Money: { Regex check = new Regex(@"(^[0-9]*[1-9][0-9]*$)|(^[0-9]\d*\.(\d{2}|\d{1})$)"); return(check.IsMatch(strInput)); }; case EnumRegularType.Price: { Regex check = new Regex(@"(^[0-9]*[1-9][0-9]*$)|(^[0-9]\d*\.(\d{3}|\d{1})$)"); return(check.IsMatch(strInput)); }; case EnumRegularType.Url: { Regex check = new Regex(@"(?<protocol>(http|ftp|gopher|telnet|file|wais)+://)"); return(check.IsMatch(strInput)); }; case EnumRegularType.PostCode: { Regex check = new Regex(@"^[1-9]\d{5}$"); return(check.IsMatch(strInput)); }; case EnumRegularType.IDCard: { Regex check = new Regex(@"(\(\d{3}\)|\d{3}-)?\d{11}\d{18}|\d{15}"); return(check.IsMatch(strInput)); }; case EnumRegularType.IP: { Regex check = new Regex(@"(?<ip>(\d{1,3}\.){3}\d{1,3})"); return(check.IsMatch(strInput)); }; case EnumRegularType.Percentage: { Regex check = new Regex(@"(^[0-9]*[1-9][0-9]*$)|(^[0-9]\d*\.(\d{2}|\d{1})$)"); if (check.IsMatch(strInput) && (decimal.Parse(strInput) <= 1 && decimal.Parse(strInput) >= 0)) { return(true); } else { return(false); } } case EnumRegularType.IntHaveZero: { Regex check = new Regex(@"^[0-9]*[0-9][0-9]*$"); return(check.IsMatch(strInput)); }; case EnumRegularType.Custom: { return(true); } default: { return(true); } } }