0

I am a college student working with Asp.net MVC, I need to get a custom validation working that prevents input of specific characters e.g @#$%*, etc for a specific field. I know custom validations are a bit of a hassle but its what this part of the assignment calls for. I don't mind that I type with one finger but college takes all the fun out of learning any programming topic or language because I can't spend as much time as I want to learn proficient implementation of concepts. In this case custom Validation.

I created an InvalidCharsAttribute Class already and have already added the necessary field to the necessary class as follows...

InvalidCharsAttribute.cs

using System.ComponentModel.DataAnnotations;


namespace EnrollmentApplication.Models
{
    public class InvalidCharsAttribute : ValidationAttribute

    {
        readonly string invalidChars;

        public InvalidCharsAttribute(string invalidChars)
        {
            this.invalidChars = invalidChars;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                if ((string)value == invalidChars)
                {

                 return new ValidationResult("Notes contains unacceptable characters");


                }
            }

            return ValidationResult.Success;
        }
    }

Field to be checked for invalid characters

 [InvalidChars(@"[*&%#@$^!]", ErrorMessage = "*&%#@$^! are invalid characters")]
        public virtual string Notes { get; set; }

I am aware that if ((string)value == invalidChars) is wrong I'm just not sure how to rewrite/add code so that my custom validation works. Simplest solution would be great.

Z. Josh
  • 5
  • 6
  • I think the simplest way to go would be using a jQuery input mask plugin and use a regex mask to prevent the invalid characters from being entered. You can make arguments for where this validation should go in the code and the proper way to validate input, but, personally, I prefer setting up the input fields with a mask to prevent any invalid input. It's pretty common to include back-end validation along with this though. – Dortimer Oct 18 '19 at 18:17
  • Additionally, for the string comparison, `string.Equals` is better than just plain `==`. Since `==` takes the casing into account when doing the comparison. With `string.Equals` you can pass some options along with the call so that it ignores the casing: https://stackoverflow.com/a/6371250/2720343 – Dortimer Oct 18 '19 at 18:21
  • @Dortimer, that might be fine as long as it's not detrimental to the system if the value *could* have invalid characters. It's pretty trivial to hack around any jQuery or other front-end solution. – ChiefTwoPencils Oct 18 '19 at 18:30
  • There are a few ways to do this, one of which is to split apart the invalid chars string into a char array that you iterate over looking for any that are contained in the value. – ChiefTwoPencils Oct 18 '19 at 18:33
  • @ChiefTwoPencils I was more or less guessing that they have a specific set of characters that won't change across the system given that it's homework. I'm not suggesting throwing them around everywhere, but there are definitely instances where it's less of a headache. – Dortimer Oct 18 '19 at 18:41
  • @ChiefTwoPencils I think I will give your suggestion a try since I am at least reasonably familiar with array iteration. – Z. Josh Oct 18 '19 at 18:59

1 Answers1

0

You can use a Regex implementation to validate your string through a regular expression. If the string you are comparing matches with any of the invalid characters then the response will be greater than zero, and in that case your code should return false:

    Regex.Matches(yourstring,@"[a-zA-Z]").Count;

The edited code would be like:

    if (value != null)
        {
            if (Regex.Matches(((string)value),@"[*&%#@$^!]").Count > 0)
            {

             return new ValidationResult("Notes contains unacceptable characters");

            }
        }

Give it a try, the Regex implementation might help you in the validation.

regards,

cvillalobosm
  • 156
  • 2
  • 12