faqts : Computers : Programming : Languages : JavaScript : Language Core : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 11 people (64%) answered Yes
Recently 5 of 9 people (56%) answered Yes

Entry

Can control characters in strings (that are invisible in form text areas) be filtered out or identified?

Sep 3rd, 2007 07:56
Dave Clark, Klaus Bolwin, Jonathan Frieder,


for (i=0; i<string.length; i++)
{
if(string.charCodeAt(i) < 32) alert("Controlcharakter at position "+i);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jonathan,
     The above method does identify every possible control character 
having an equivalent decimal value less than that of the space 
character (i.e., 32 possible such control characters from x'00' 
through x'1F').  The likelyhood of needing to account for every 
possible control characters is not the usual case, though.  Thus, the 
following method is faster because it requires no loop on a character-
by-character basis -- though it strips out only the common control 
characters:
mystring = mystring.replace(/[\x00\f\n\r\t\v]/g, '');
     That single statement uses a Regular Expression to automatically 
match on every null, form-feed, new-line, carriage-return, horizontal-
tab, and vertical-tab character in the string and strip it out.
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~