|
-
Feb 13th, 2004, 07:11 AM
#1
Thread Starter
Frenzied Member
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
-
Feb 13th, 2004, 07:23 AM
#2
Frenzied Member
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. 
-
Feb 13th, 2004, 07:25 AM
#3
Thread Starter
Frenzied Member
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
-
Feb 13th, 2004, 07:34 AM
#4
Frenzied Member
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. 
-
Feb 13th, 2004, 07:46 AM
#5
Thread Starter
Frenzied Member
Blimey that was quick!
Thanks I'll give it go!
DJ
-
Feb 13th, 2004, 07:52 AM
#6
Frenzied Member
was it how you wanted it?
Have I helped you? Please Rate my posts. 
-
Feb 13th, 2004, 07:59 AM
#7
Thread Starter
Frenzied Member
It works absolutely perfectly apart from for some reason it doesn't encode _ (underscore) - any ideas why?
-
Feb 13th, 2004, 08:00 AM
#8
Frenzied Member
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. 
-
Feb 13th, 2004, 10:32 AM
#9
Thread Starter
Frenzied Member
Excellent thanks for all your help.
DJ
-
Feb 13th, 2004, 11:10 AM
#10
Thread Starter
Frenzied Member
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
-
Feb 13th, 2004, 11:16 AM
#11
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|