Results 1 to 2 of 2

Thread: Special Chars Catch n Convert

  1. #1
    Join Date
    Oct 2008
    Location
    Pretoria, South Africa
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Special Chars Catch n Convert

    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?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

    Code:
    /x/g, '×'
    could be modified in an attempt to prevent this, like:

    Code:
    /(\d *)x( *\d)/g, '$1×$2'
    Which would be ideal for stuff like:

    255 x 150

    and:

    75x22

    but would leave stuff like the x in:

    exact

    alone.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •