Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Feb 5, 2024, 9:19 AM EST
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:
public class MyModel { [DataType(DataType.MultilineText)] [DisallowHtml] public string Description { get; set; } }[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.Jithu ThomasRank #437 · 5.7K reputationPosted Feb 5, 2024, 8:21 AM EST
$.validator.addMethod("noHtml", function (value, element) { return !/]*>/g.test(value); }, "HTML tags are not allowed."); $(document).ready(function () { $("#YourTextBoxId").rules("add", { noHtml: true }); });Replace
YourTextBoxIdwith the actual ID of your TextBox.Yogi SRank #511 · 4.8K reputationPosted Feb 5, 2024, 8:47 AM EST
You can do html encoding of text or use reqular expression to check for html tag entries.