Esempio n. 1
0
        //validation module
        public string this[string parameter]
        {
            get
            {
                string error = null;

                switch (parameter)
                {
                case "Username":
                    Regex regex = new Regex(regexUsername);
                    Match match = regex.Match(Username);

                    if (string.IsNullOrWhiteSpace(Username))
                    {
                        error           = "Username is required.";
                        IsValidUsername = false;
                    }
                    else if (!match.Success)
                    {
                        error = "Username must be valid username format and contains only alphabetic symbols and numbers.\n" +
                                "For example 'Cyberprank2020'";
                        IsValidUsername = false;
                    }
                    else if (Username.Length < MIN_USERNAME_LENGTH || Username.Length > MAX_USERNAME_LENGTH)
                    {
                        error           = "Username length nust be no less than 6 symbols and no more than 20 symbols.";
                        IsValidUsername = false;
                    }
                    else
                    {
                        IsValidUsername = true;
                    }

                    break;

                case "IP":
                    regex = new Regex(regexIP);
                    match = regex.Match(IP);

                    if (string.IsNullOrWhiteSpace(IP))
                    {
                        error     = "IP is required";
                        IsValidIP = false;
                    }
                    else if (!match.Success)
                    {
                        error     = "IP must be valid ip format. For example '192.168.1.0'";
                        IsValidIP = false;
                    }
                    else
                    {
                        IsValidIP = true;
                    }

                    break;

                case "Port":
                    if (string.IsNullOrWhiteSpace(Port))
                    {
                        error       = "Port is required";
                        IsValidPort = false;
                    }
                    else if (!Port.All(char.IsDigit))
                    {
                        error       = "Port must be valid";
                        IsValidPort = false;
                    }
                    else
                    {
                        IsValidPort = true;
                    }

                    break;
                }

                if (Errors.ContainsKey(parameter))
                {
                    Errors[parameter] = error;
                }
                else if (error != null)
                {
                    Errors.Add(parameter, error);
                }

                RaisePropertyChanged(nameof(Errors));
                return(error);
            }
        }