locked
How to validate these kinds of special characters? RRS feed

  • Question

  • User-1651604128 posted

    In my razor view of MVC web app, I have some text fields, user requires these characters are not allowed to entered: "7%;&@$&^*!()", it seems only accept the English characters.

    Is there any easy validate rule to be used?

    Thanks

    Monday, April 20, 2020 4:07 PM

Answers

All replies

  • User475983607 posted

    Is there any easy validate rule to be used?

    Use a standard RegEx data annotation that matches characters and spaces; https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.regularexpressionattribute?view=netframework-4.8

    ^[a-zA-Z\s]*$

     

    Monday, April 20, 2020 4:19 PM
  • User-1651604128 posted

    Peter Cong

    Is there any easy validate rule to be used?

    Use a standard RegEx data annotation that matches characters and spaces; https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.regularexpressionattribute?view=netframework-4.8

    ^[a-zA-Z\s]*$

     

    Hi mgebhard, thanks a lot for your quick response,

    But I need to integrate your regular express validation logic to my current validationAttribute as the code below in my viewModel class, any idea to do it? thanks a lot

     
    [CheckEnglishCode]
             public string ENGLISH_CODE { get; set; }
    
    ......
    public class CheckEnglishCode : ValidationAttribute
        {
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                var model = (ProdViewModel)validationContext.ObjectInstance;
    
                if ((model.Prod_Status != "P") && (model.ENGLISH_CODE == null))
                {
                    return new ValidationResult(Product.App_LocalResources.GlobalRes.EngCodeIsRequired );
                }
    
                return ValidationResult.Success;
            }
        }

    Monday, April 20, 2020 5:38 PM
  • User475983607 posted

    Peter Cong

    But I need to integrate your regular express validation logic to my current validationAttribute as the code below in my viewModel class, any idea to do it?

    Your original post states that more than one property needs this validation.   I would add the RegEx validation annotation to the properties you wish to apply this validate rather than adding it to a custom validation attribute.  Follow standard SOLID principles.  

    Monday, April 20, 2020 6:03 PM
  • User-1651604128 posted

    Yes, I have 5 different text fields with the same validation logic and need to add the same regular expression to each of them. I guess I need to integrate the Regression expression to the existing validation logic.

    Basically, the logic is simplified as this:

    if ((model.Status != "P") && (model.ENGLISH_CODE == null))

    {

    return new ValidationResult("English Code is required");

    } else {

    //Apply the regular expression validation here.

    }

     

    Monday, April 20, 2020 6:16 PM
  • User475983607 posted

    Below illustrates how to use the .NET RegEx API.

    string pattern = @"^[a-zA-Z\s]*$";
    Regex regex = new Regex(pattern);
                
    string test = "@Hello";
    Console.WriteLine($"{test} = {regex.IsMatch(test)}");
    
    test = "Hello";
    Console.WriteLine($"{test} = {regex.IsMatch(test)}");

    Result

    @Hello = False
    Hello = True
    

    Reference documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch?view=netframework-4.8
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2020 6:38 PM
  • User-1651604128 posted

    thanks a lot, this works great,

    Tuesday, April 21, 2020 12:12 AM