Display Friendly Error Message When Html tag is entered in a text Box- MVC ASP.NET

后端 未结 3 809
Happy的楠姐
Happy的楠姐 2021-01-07 08:30

I have requirement of validating user input in a text box. Whenever a html tag is entered it should display the same view with friendly error message like \"Cannot enter htm

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 09:11

    It is working now by displaying the friendly error message. I have changed a little bit by adding Validateinput tag at the Post Action controller.

    I have to add this in ViewModel

    [AllowHtml]
    [RegularExpression (@"^[^<>]*$", ErrorMessage = "You have entered html... Html is not a valid input!" )]
    public string SomePropertyThatShouldNotAcceptHtml { get; set; }
    

    In Action Controller

    I have to add the tag in the Post Event

    [Validateinput(false)]
    

    Thanks Darin.

提交回复
热议问题