Results 1 to 4 of 4

Thread: Script to find and replace characters

  1. #1
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Script to find and replace characters

    Can anybody help me with a script to examine input on a web form and replace the following characters as follows.

    Character Replace with
    Ñ
    ñ ñ
    á á
    é é
    í í
    ó ó
    ú ú
    & &
    £ sterling
    € euros

    All the vowels above are accented.

    I would like to pass the contents of a text input box e.g. <input type="text" name="name"> into a function and have the function do the replacements (if any).
    Cheers
    Billy

  2. #2
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    The replacement characters should be the numeric value (I need to space the value to prevent the browser rendering the characters)
    ñ & # 241;
    á & # 225;
    é & # 233;
    í & # 237;
    ó & # 243;
    ú & # 250;
    Cheers
    Billy

  3. #3
    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

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>Character Replacement Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    var str='Ñ ñ á é í ó ú & £ €';
    function rep(t){
    var t=t;
    for (var i = 0; i < rep.ar.length; i++)
    t=t.replace(rep.ar[i++], rep.ar[i]);
    return t;
    }
    rep.ar=[
    //unicode | entity/string
    /\u0026/g, '&amp;',  //the ampersand (&) must be first or else it will be replaced in other replacements
    /\u00d1/g, '&#209;',
    /\u00f1/g, '&#241;',
    /\u00e1/g, '&#225;',
    /\u00e9/g, '&#233;',
    /\u00ed/g, '&#237;',
    /\u00f3/g, '&#243;',
    /\u00fa/g, '&#250;',
    /\u00a3/g, 'sterling',
    /\u20ac/g, 'euros'  //note - no comma after last entry
    ];
    </script>
    </head>
    <body>
    <form name="repform" action="#">
    <input name="vals" type="hidden"><br>
    <input type="submit" value="Go!">
    </form>
    <script type="text/javascript">
    document.forms['repform']['vals'].value=rep(str);
    </script>
    <input type="button" value="Alert" onclick="alert(rep(str));">
    </body>
    </html>
    - John
    ________________________

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

  4. #4
    Join Date
    Jul 2005
    Posts
    101
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks John. Got it working in my form without problems. Thanks again.
    Cheers
    Billy

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
  •