コード例 #1
0
        /// <summary>
        /// Determines whether a string contains only printable characters.
        /// </summary>
        /// <param name="stringToCheck">The string to check.</param>
        /// <returns>
        /// <c>true</c> if the string contains only printable characters; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsPrintableCharacter(this string stringToCheck)
        {
            if (string.IsNullOrEmpty(stringToCheck))
            {
                return(false);
            }

            // get the alphabet provider
            IAlphabetProvider provider = new EnglishAlphabetProvider();

            // get the printable character set
            var result = provider.GetPrintableCharacters().ToArray();

            // return false if the character in the string is not found in printable character set
            return(stringToCheck.All(result.Contains));
        }
コード例 #2
0
        /// <summary>
        /// Validates the specified value with respect to the current validation attribute.
        /// </summary>
        /// <param name="value">
        /// The value to validate.
        /// </param>
        /// <param name="validationContext">
        /// The context information about the validation operation.
        /// </param>
        /// <returns>
        /// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
        /// </returns>
        protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
        {
            if (value.IsNull())
            {
                return(ValidationResult.Success);
            }

            string convertedValue;

            if (validationContext.IsNull())
            {
                validationContext = new ValidationContext(value, null, null)
                {
                    DisplayName = "The value"
                };
            }

            var memberNames = new[] { validationContext.MemberName };

            try
            {
                convertedValue = Convert.ToString(value);
            }
            catch (FormatException)
            {
                this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeString);

                return(new ValidationResult(this.ErrorMessage));
            }

            if (!convertedValue.IsPrintableCharacter())
            {
                var    englishAlphabetProvider = new EnglishAlphabetProvider();
                string extendedCharacters      = string.Join(string.Empty, englishAlphabetProvider.GetPrintableCharacters());

                this.ErrorMessage = string.Format(ValidationMessages.CanOnlyContainPrintableCharacters, validationContext.DisplayName, extendedCharacters);

                return(new ValidationResult(this.ErrorMessage, memberNames));
            }

            return(ValidationResult.Success);
        }
コード例 #3
0
		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			string convertedValue;
			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new[] { validationContext.MemberName };

			try
			{
				convertedValue = Convert.ToString(value);
			}
			catch (FormatException)
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeString);

				return new ValidationResult(this.ErrorMessage);
			}

			if (!convertedValue.IsPrintableCharacter())
			{
				var englishAlphabetProvider = new EnglishAlphabetProvider();
				string extendedCharacters = string.Join(string.Empty, englishAlphabetProvider.GetPrintableCharacters());

				this.ErrorMessage = string.Format(ValidationMessages.CanOnlyContainPrintableCharacters, validationContext.DisplayName, extendedCharacters);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
コード例 #4
0
		public void ReturnPrintableCharacters()
		{
			// Arrange
			var expectedCharacters = new List<char>();

			// Ascii values of all upper and lower case alphabets, numbers 0 - 9 and special characters till the max ascii value of 126 
			for (int i = 32; i <= 126; i++)
			{
				// convert to a character
				char c = Convert.ToChar(i);

				// add to the list
				expectedCharacters.Add(c);
			}

			IAlphabetProvider provider = new EnglishAlphabetProvider();

			// Act
			var result = new string(provider.GetPrintableCharacters().ToArray());

			// Assert
			Assert.AreEqual(expectedCharacters, result);
		}
コード例 #5
0
        public void ReturnPrintableCharacters()
        {
            // Arrange
            var expectedCharacters = new List <char>();

            // Ascii values of all upper and lower case alphabets, numbers 0 - 9 and special characters till the max ascii value of 126
            for (int i = 32; i <= 126; i++)
            {
                // convert to a character
                char c = Convert.ToChar(i);

                // add to the list
                expectedCharacters.Add(c);
            }

            IAlphabetProvider provider = new EnglishAlphabetProvider();

            // Act
            var result = new string(provider.GetPrintableCharacters().ToArray());

            // Assert
            Assert.AreEqual(expectedCharacters, result);
        }
コード例 #6
0
        /// <summary>
        /// Determines whether a string contains only printable characters.
        /// </summary>
        /// <param name="stringToCheck">The string to check.</param>
        /// <returns>
        /// <c>true</c> if the string contains only printable characters; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsPrintableCharacter(this string stringToCheck)
        {
            if (string.IsNullOrEmpty(stringToCheck))
            {
                return false;
            }

            // get the alphabet provider
            IAlphabetProvider provider = new EnglishAlphabetProvider();

            // get the printable character set
            var result = provider.GetPrintableCharacters().ToArray();

            // return false if the character in the string is not found in printable character set
            return stringToCheck.All(result.Contains);
        }