Exemple #1
0
        private bool IsFormValid()
        {
            if ((!string.IsNullOrEmpty(NameOnCard)) && (!string.IsNullOrEmpty(CardNumber)) && (!string.IsNullOrEmpty(CVC)) && (!string.IsNullOrEmpty(ExpiryDate)) && (!string.IsNullOrEmpty(ZipCode)))
            {
                Regex nameRegex  = new Regex(@"^[\w\s]*$");
                Regex monthRegex = new Regex(@"^(0[1-9]|1[0-2])$");
                Regex yearRegex  = new Regex(@"^(19|20)\d{2}$");

                if (!nameRegex.IsMatch(NameOnCard))
                {
                    Message = "Name is invalid";
                    return(false);
                }

                if ((CardNumber.Length < 15) || (CardNumber.Length > 16))
                {
                    Message = "Credit card number length is invalid";
                    return(false);
                }

                if (!long.TryParse(CardNumber, out _cardNumberLong))
                {
                    Message = "Credit card number is invalid";
                    return(false);
                }

                if (!CVC.Length.Equals(3))
                {
                    Message = "CVC is inavlid";
                    return(false);
                }

                if (!Int32.TryParse(CVC, out _cvcInt))
                {
                    Message = "CVC is invalid";
                    return(false);
                }

                if (ExpiryDate.Contains("/"))
                {
                    string month = ExpiryDate.Split('/')[0];
                    string year  = ExpiryDate.Split('/')[1];

                    if ((!monthRegex.IsMatch(month)) || (!yearRegex.IsMatch(year)))
                    {
                        Message = "Invalid expiration date";
                        return(false);
                    }
                    else
                    {
                        if ((!Int32.TryParse(month, out int monthInt)) || (!Int32.TryParse(year, out int yearInt)))
                        {
                            Message = "Invalid expiration date";
                            return(false);
                        }
                    }
                }
                else
                {
                    Message = "Invalid expiration date";
                    return(false);
                }

                if (!ZipCode.Length.Equals(5))
                {
                    Message = "Zip Code length is invalid";
                    return(false);
                }

                if (!Int32.TryParse(ZipCode, out _zipCodeInt))
                {
                    Message = "Zip Code is invalid";
                    return(false);
                }

                Message = string.Empty;
                return(true);
            }
            else
            {
                return(false);
            }
        }