adsense

Monday, December 28, 2015

Asp.net textbox allow < charactor to be saved

Normally, saving special characters such as < or > require special configuration settings to be enabled in web.config file and in the page itself.


1) Disable request validation in the page

<%@ Page title="test" .... ValidateRequest="false" %>

2) Set requestValidationMode in Web.config


      



This solution could expose unexpected security threads.  Jquery provides a nice workaround to overcome this issue by escaping < and > characters as shown below.

 function validateTxt() {
    $("textarea, input[type='text']").change(function () { //replaces in all text areas and textboxes
      html = $(this).val(); //get the value
      //.replace("a" , "b")  works only on first occurrence of "a"
      html = html.replace(/< /g, "<"); //before: if there's space after < remove
      html = html.replace(/
      $(this).val(html); //set new value
   });
}

$(function () {
      validateTxt();
});
More discussion regarding the topic can be found here.

Cheers,
Samitha

 

No comments:

Post a Comment