1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- namespace GDNXFD.Alert.Config.Validations
- {
- using Data.Repositories;
- using Data;
- using System;
- using System.Windows.Controls;
- using System.Text.RegularExpressions;
- /// <summary>
- /// Rule for required fields
- /// </summary>
- public class AlertRuleIDValidationRule : ValidationRule
- {
- public static bool Enabled = true;
- /// <summary>
- /// When overridden in a derived class, performs validation checks on a value.
- /// </summary>
- /// <param name="value">The value from the binding target to check.</param>
- /// <param name="cultureInfo">The culture to use in this rule.</param>
- /// <returns>
- /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
- /// </returns>
- public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
- {
- if (!Enabled)
- return new ValidationResult(true, null);
- if (value == null || String.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(false, "不能为空!");
- string id = value.ToString();
- if (id.Length < 5)
- return new ValidationResult(false, "至少输入5个字母或数字!");
- if (!IsNatural_Number(id))
- return new ValidationResult(false, "只能输入字母或数字,不能使用特殊字符!");
- AlertRule ar = AlertRuleRepository.GetAlertRuleById(id);
- if (ar != null)
- return new ValidationResult(false, "编码已存在,请重新输入!");
- return new ValidationResult(true, null);
- }
- private bool IsNatural_Number(string strNum)
- {
- Regex notWholePattern = new Regex(@"^[0-9a-zA-Z\$]+$");
- return notWholePattern.IsMatch(strNum, 0);
- }
- }
- }
|