Results 1 to 11 of 11

Thread: JavaScript characters

  1. #1

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    JavaScript characters

    I have a string that I'd like to replace all characters other than A-Z, a-z and 0-9 with enchar### where ### is the ascii code for the character e.g. 42 = *.

    RegExp seems to be the way to go but I'm not sure how to use it to do this.

    Any help appreciated.

    DJ

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    let me just see if I get what you mean.
    this is me(
    would become:
    thisenchar(ASCII for space)isenchar(ASCII for space)me(ASCII for open bracket)?
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    You are a *

    would become:

    You are a enchar042

    or even

    You are a enchar42

    I'm not too bothered about the leading zero.

    HTH

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>::Acid</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function doIt(me) {
    string = me.value
    output = ""
    regexp = /\w/gi
    space = " "
    for (i=0; i<string.length; i++)
    	{
    	if (string.substr(i,1).search(regexp) == 0 || string.substr(i,1).search(space) == 0)
    		{
    		output += string.substr(i,1)
    		}
    	else
    		{
    		output += "enchar"+string.charCodeAt(i)
    		}
    	}
    me.value = output
    }
    </script>
    </head>
    
    <body>
    <input name="myTxt" type="text" id="myTxt" onBlur="doIt(this)">
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  5. #5

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Blimey that was quick!

    Thanks I'll give it go!

    DJ

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    was it how you wanted it?
    Have I helped you? Please Rate my posts.

  7. #7

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    It works absolutely perfectly apart from for some reason it doesn't encode _ (underscore) - any ideas why?

  8. #8
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    because /w (in the regexp) hold A-Z,a-z,1-9 and _.

    You could make another IF function, if it is an underscore, then add the charCode of it there.
    Have I helped you? Please Rate my posts.

  9. #9

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Excellent thanks for all your help.

    DJ

  10. #10

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131
    Acidic thanks for all your help again.

    My code in full is now:
    Code:
    function encode_special_characters(formname) {
    
    	var totalElements = window.document.forms[formname].elements.length;
    	
    	for (elementNumber = 0; elementNumber < totalElements; elementNumber++) {
    		if (window.document.forms[formname].elements[elementNumber].type == "text" || window.document.forms[formname].elements[elementNumber].type == "textarea") {
    			inText = window.document.forms[formname].elements[elementNumber].value;
    			outText = "";
    			regexp = /\w/gi;
    			space = " ";
    			for (i=0; i < inText.length; i++) {
    				if ((inText.substr(i,1).search(regexp) == 0 || inText.substr(i,1).search(space) == 0) && !inText.substr(i,1).search("_") == 0) {
    					outText += inText.substr(i,1);
    				} else {
    					outText += "enchar" + inText.charCodeAt(i);
    				}
    			}
    			window.document.forms[formname].elements[elementNumber].value = outText;
    		}
    	}
    }
    Which works perfectly.

    Thanks again.

    DJ

  11. #11
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    no problem
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width