How to Restrict User to Put HTML Tag into TextBox with mvc5.
Loading
How to Restrict User to Put HTML Tag into TextBox with mvc5.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jayraj ChhayaPosted Feb 5, 2024, 2:19 PM
you can use the built-in validation features provided by the framework. One approach is to use the
[AllowHtml]attribute to explicitly allow HTML input for specific properties in your model, while disallowing it for others.Here's an example of how you can achieve this:
[DataType(DataType.MultilineText)]attribute. This attribute is used to specify that the property is a multiline text input.Html.TextAreaForhelper method to render the TextBox for the property.By using the
[DisallowHtml]attribute, any HTML tags entered by the user will be automatically stripped out when the form is submitted. This helps prevent potential security vulnerabilities such as cross-site scripting (XSS) attacks.Yogi SPosted Feb 5, 2024, 1:47 PM
You can do html encoding of text or use reqular expression to check for html tag entries.
Jithu ThomasPosted Feb 5, 2024, 1:21 PM
Replace
YourTextBoxIdwith the actual ID of your TextBox.