0

Special Characters

! @ # $ % ^ & * ( ) _ + = - { [ } ] : ; " ' | \ < , > . ? /

I have a simple saving of data. I notice that if I enter all special characters in a textbox and not using a escape method all special character after the & sign is being cut.

With Out Escpae

Output

! @ # $ % ^

With Escape

Here

JS

var txt = $("txtbox").val();

Notice that the output has a %20% how I remove it?.

  • What is the "simple saving of data" that you are doing? Are you using this string as part of a URL? In that case, yes, since `&` is a reserved character in a URL, you will need to escape it (using `encodeURIComponent`). Yes, escaping will also escape spaces. Normally, this will automatically be decoded correctly on the server so you don't have to worry about it. –  Sep 26 '16 at 03:46
  • @torazaburo simple saving like.. `Data = A & "B"` something like that of data –  Sep 26 '16 at 03:47
  • So by "saving" you mean storing into a variable? What kind of syntax is that? It doesn't look like JavaScript. What is "A" in this statement? Which part of it corresponds to the value from the text box? –  Sep 26 '16 at 03:49
  • @torazaburo sorry if you don't understand it. `var txtboxvalue = $("#txtboxID").val(); ----- the value is A & "B"` that's why i ask how can i save the special characters.. But is okay it solve now. –  Sep 26 '16 at 03:56

1 Answers1

2

%20 is space. If you wish to remove it, you can do it like,

Note that escape is deprecated, Use encodeURIComponent instead.

var str = `! @ # $ % ^ & * ( ) _ + = - { [ } ] : ; " ' | \ < , > . ? /`;
var encoded = encodeURIComponent(str.split(' ').join(''));
// or encodeURIComponent(str.replace(/\s+/g, ''));
alert(encoded);

And here's your fiddle

choz
  • 17,242
  • 4
  • 53
  • 73
  • Can the mvc3 controller read all that input Special Character if I use your method? –  Sep 26 '16 at 03:15
  • @KiYu In ASP.Net, you should be able to use [HttpServerUtility.UrlDecode](https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urldecode.aspx) to parse it on controller or view level. I havent been in asp.net for a while now. But I believe they would have their own encoder and decoder for input and output. – choz Sep 26 '16 at 03:20
  • 2
    thanks to your solution and thanks to the accepted answer [Here](http://stackoverflow.com/questions/6022832/how-do-i-decode-html-that-was-encoded-in-js-using-encodeuricomponent) for decoding the uri. –  Sep 26 '16 at 03:40
  • If `escape` is deprecated and `encodeURIComponent` is preferred, then put that in your answer, not as a note. But you have left the basic question unanswered, which is why would he not want to pass the spaces, when the user went to all the trouble to enter them in the text box? –  Sep 26 '16 at 03:55
  • @torazaburo Got it. For the spaces, I assume that OP does not type the values but instead got it from somewhere else like xhr gets or reading it from document element value? – choz Sep 26 '16 at 04:00
  • @torazaburo sorry about the spaces.. I don't know that it can be escape also. –  Sep 26 '16 at 04:02