Wednesday, 3 September 2014

How to disable html tags inserting in a TextBox

Javascript function
    <script type="text/javascript">

    var IsShiftPressed = false;
    function PreventHtml(Sender, e) {
        var key = e.which ? e.which : e.keyCode;
        // 16 code stands for shift key
        if (key == 16) {
            IsShiftDown = true;
        }
        // 188 code for '<', 190 for '>', 53 for '%' and 54 for '^'
        else if ((IsShiftDown == true) && ((key == 188) || (key == 190) || (key == 53) || (key == 54))) {
            return false;
        }
    }
    </script>

<asp:TextBox ID="txtPreventHtmlTags" runat="server" onkeydown="return PreventHtml(this,event);" />
That’s it!!…..Happy Programming...