DataAnnotations and Regular Expression not working with encoded characters

Nicolas Liebel 121 Reputation points
2022-06-20T19:50:23.34+00:00
[RegularExpression(@"^(\s)|(<.*?>)|(mailto:|http:|https:|ftp:|ftps:|<|\/\/)", ErrorMessageResourceType = typeof(Std), ErrorMessageResourceName = "Validation_WrongFormat")]  
        [Display(Name = "lbl_Comment", ResourceType = typeof(Resource1))]  
        [Required(ErrorMessageResourceType = typeof(Std), ErrorMessageResourceName = "Validation_Required")]  
        public string Comment { get; set; }  

Generated on my dotnet 6 web razor page:
<textarea rows="5" style="min-width:100%" data-val="true" data-val-regex="The format is wrong, please modify it and try again." data-val-regex-pattern="^(\s)|(<.*?>)|(mailto:|http:|https:|ftp:|ftps:|<|//)$" data-val-required="The field Comment is required." id="Comment" name="Comment">
</textarea>

The all idea is to not allow any HTML Tag and also no mailto:, https:, etc. string in the textarea control using the regularexpression. Of course the text may have some newline and carriage return.
So far it always returning me an "The format is wrong, please modify it and try again." . I think that the encoding is the issue or something but can't figure this out.
Thanks for the help.

Developer technologies ASP.NET ASP.NET Core
Developer technologies C#
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-06-20T20:03:33.183+00:00

    Try this pattern: @"(?s)^(?!\s*(mailto:|https?:|ftps?:)//)((?!<.*?>).)*$".

    However, such input limitations should probably not exist.

    1 person found this answer helpful.
    0 comments No comments

  2. Nicolas Liebel 121 Reputation points
    2022-06-20T20:37:03.843+00:00

    Sorry, it doesn't work as it gives an error on the site:
    Uncaught SyntaxError: Invalid regular expression: /(?s)^(?!\s*(mailto:|https?:|ftps?:)//)((?!<.?>).)$/: Invalid group (at jquery.validate.unobtrusive.min.js:5:3856)

    Works ok in regex tester like many other but not from the DataAnnotation from my class in C# used on the razor page generated
    <textarea asp-for="Comment" rows="5"></textarea>

    Also Yes such limitation can be existing as to prevent people to send html code within this text area or unwanted email or html tags :-(

    1 person found this answer helpful.
    0 comments No comments

  3. Viorel 122.5K Reputation points
    2022-06-20T20:41:20.193+00:00

    Check an alternative that should work in browsers too: @"^(?!\s*(mailto:|https?:|ftps?:)//)((?!<.*?>)(.|\r|\n))*$".

    1 person found this answer helpful.
    0 comments No comments

  4. Nicolas Liebel 121 Reputation points
    2022-06-20T21:48:06.767+00:00

    Thank Viorel-1 to direct me to the right direction.
    The code provided did not work but close enough so that I found the solution with this version:

    @"^((?!<.?>|mailto:|ftps?:|https?:|//|<)(.|\r|\n))$

    Thanks again

    0 comments No comments

  5. Rijwan Ansari 766 Reputation points MVP
    2022-06-21T02:22:40.337+00:00

    HI @Nicolas Liebel

    You can use https://regex101.com/ to test, debug and validate your regex.

    213162-screenhunter-33.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.