Tuesday 30 September 2014

How to disable/Prevent/Don’t allow the User from Entering HTML tags in Textbox/TextArea

Hi Friends,in this article I will explain about How to disable/Prevent/Don’t allow the User from Entering HTML tags in Textbox/TextArea.
When you enter html tags as shown below figure and click on submit button 

then "A potentially dangerous Request.Form value was detected from the client" error will come.



To overcome this ,if we don’t allow HTML tags then the error is not coming.
Note: Always HTML tags begin with a less-than sign: < and end with a greater-than sign: >. So,If we restrict < and > symbols then issue will be solved.
To prevent the users entering HTML Tags ,we restrict the open(<) and close(>) tags characters for textbox.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to disable/Prevent/Don't allow User from Entering HTML tags in Textbox/TextArea
    </title>
    <script type="text/javascript" language="javascript">
        var IsShiftPressed = false;
        function DisableHTMLTags(Sender, e) {
            var key = e.which ? e.which : e.keyCode;
            if (key == 16) {
                IsShiftPressed = true;
            }
            else if ((IsShiftPressed == true) && ((key == 188) || (key == 190))) {
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Name:
        <asp:TextBox ID="txtDontAllowHTMLTags" runat="server" onkeydown="return DisableHTMLTags(this,event);" />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>


The output of the above code as shown in the below figure.While using the above code we can restrict < or > symbols.

You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.