Exemplo n.º 1
0
        public KeypadParseResult Validate(string text)
        {
            var result = new KeypadParseResult();
            var type   = this.Type;

            if (type == KeypadType.Number)
            {
                var cause = NumberRange.TryParse(text, this.ToNumberRange(), out var value);
                result.NumberErrorCause = cause;
                result.Number           = value;
                result.Validated        = cause == NumberErrorCause.None;
            }
            else if (type == KeypadType.IPAddress)
            {
                IPAddress value  = null;
                var       cause  = IPAddressErrorCause.None;
                var       splits = text.Split('.');

                if (splits.Length == 4)
                {
                    var anyDecimalValidated = splits.All(t => { return(byte.TryParse(t, out var b)); });

                    if (anyDecimalValidated == true)
                    {
                        if (IPAddress.TryParse(text, out value) == false)
                        {
                            cause = IPAddressErrorCause.ParseError;
                        }
                    }
                    else
                    {
                        cause = IPAddressErrorCause.AnyDecimalInvalid;
                    }
                }
                else
                {
                    cause = IPAddressErrorCause.DecimalCountInvalid;
                }

                result.Validated      = cause == IPAddressErrorCause.None;
                result.IPAddressCause = cause;
                result.IPAddress      = value;
            }
            else if (type == KeypadType.String)
            {
                result.Validated = true;
                result.String    = text;
            }

            var e = new KeypadValidateEventArgs(this, result, text);

            this.OnValidate(e);
            return(result);
        }
Exemplo n.º 2
0
 protected virtual void OnValidate(KeypadValidateEventArgs e)
 {
     this.Validating?.Invoke(this, e);
 }