using System; using System.Text.RegularExpressions; namespace BgTools.Dialogs { public class TextValidator { public enum ErrorType { Invalid = -1, Info = 0, Warning = 1, Error = 2 } [NonSerialized] public ErrorType m_errorType = ErrorType.Invalid; [NonSerialized] private string m_regEx = string.Empty; [NonSerialized] private Func m_validationFunction; [NonSerialized] public string m_failureMsg = string.Empty; /// /// Validator for TextFieldDialog based on regex. /// /// Categorie of the error. /// Message that described the reason why the validation fail. /// String with regular expression. It need to describe the valid state. public TextValidator(ErrorType errorType, string failureMsg, string regEx) { m_errorType = errorType; m_failureMsg = failureMsg; m_regEx = regEx; } /// /// Validator for TextFieldDialog based on regex. /// /// Categorie of the error. /// Message that described the reason why the validation fail. /// Function that validate the input. Get the current input as string and need to return a bool. Nedd to return 'false' if the validation fails. public TextValidator(ErrorType errorType, string failureMsg, Func validationFunction) { m_errorType = errorType; m_failureMsg = failureMsg; m_validationFunction = validationFunction; } public bool Validate(string srcString) { if (m_regEx != string.Empty) return Regex.IsMatch(srcString, m_regEx); else if (m_validationFunction != null) return m_validationFunction(srcString); return false; } } }