View Full Version : Special Chars Catch n Convert

11-18-2008, 07:49 PM
I need to catch special chars in a textbox (which holds innerHTML) before form submit. However the input of these special chars cannot be controlled so the problem is that these chars are already in symbol form and not in ascii form. How can I convert x to &times without destroying the rest of the html?

11-18-2008, 09:11 PM
A text box cannot hold innerHTML. It holds a string value, which could potentially become innerHTML. It would be more standard to create and/or clone nodes to create functional markup from the string entered into a textbox.

That said, the operative information here is that the characters in the text box are the string value of that text box, so you could do:

var myString = textBox.value.replace(/x/g, '×');

to replace all lower case x characters in the string value to the × entity and store the new string value in the variable myString. All further processing could then be done utilizing the value of myString.

But if there are other lower case x characters that are not intended as × entities, this may be a problem, but the regular expression and replacement:

/x/g, '×'

could be modified in an attempt to prevent this, like:

/(\d *)x( *\d)/g, '$1×$2'

Which would be ideal for stuff like:

255 x 150



but would leave stuff like the x in: