Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb How to Handle Unicode Characters

    I written below javascript function to handle Unicode characters in HTML text boxes.

    code for & and ' ' goes into infinite loop, is there any better way to achive this ?

    function handleSpecialCharacters(){
    var text = document.getElementById('Article_Title').value;
    var text1 = "";
    var iChars = "<>\":[]{}`\';()@$#%&";
    for (var i = 0; i < text.length; i++) {
    if (iChars.indexOf(text.charAt(i)) != -1){
    var str = iChars.charAt(iChars.indexOf(text.charAt(i)));
    if(str == '<'){
    alert("<");
    text = text.replace(str,"&lt;");
    }
    if(str == '>'){
    alert(">");
    text = text.replace(str,"&gt;");
    }
    if(str == '&'){
    alert("&");
    text = text.replace(str,"&amp;");
    }
    if(str == ' '){
    text = text.replace(str,"&nbsp;");
    }
    if(str == '"'){
    alert("double quote");
    text = text.replace(str,"&quot;");
    }
    if(str == '''){
    text = text.replace(str,"&apos;");
    }

    }
    }
    alert("Modified Text : " +text);
    }


    thanks for you help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Your problem is that you are changing the value of text by placing & characters in it which are then found by the next pass of the loop, and also screwing up the character count. So you must output the results to a different variable modtext, and temporarily replace & by something else such as ~, and then at the end change all ~ to &.

    Try this:-


    Code:
    <script type = "text/javascript">
    
    function handleSpecialCharacters() { 
    var text = "The&cat>sat<on&the<mat";
    var modtext = text;
    var iChars = "<>\":[]{}`\';()@$#%&"; 
    
    for (var i = 0; i < text.length; i++) { 
    if (iChars.indexOf(text.charAt(i)) != -1){ 
    var str = iChars.charAt(iChars.indexOf(text.charAt(i))); 
    
    if(str == '&') { 
    modtext = modtext.replace(str,"~amp;"); 
    } 
    
    if(str == '<') { 
    modtext = modtext.replace(str,"~lt;"); 
    } 
    
    if(str == '>') { 
    modtext = modtext.replace(str,"~gt;"); 
    } 
    
    if(str == ' ') { 
    modtext = modtext.replace(str,"~nbsp;"); 
    } 
    
    if(str == '"') { 
    modtext = modtext.replace(str,"~quot;"); 
    } 
    
    if(str == "'") { 
    modtext = modtext.replace(str,"~apos;"); 
    } 
    }
    
    } 
    
    modtext = modtext.replace(/\~/g, "&");
    alert ("Modified Text : " + modtext); 
    
    } 
    
    </script>

    Note that if(str == '''){ must be if(str == "'"){


    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
    Last edited by Philip M; 01-20-2009 at 02:09 PM.

  • Users who have thanked Philip M for this post:

    sachinonnet (01-20-2009)


  •  

    Tags for this Thread

    Posting Permissions

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