PDA

View Full Version : Script to find and replace characters



wkenny
03-01-2007, 12:49 PM
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).

wkenny
03-01-2007, 12:53 PM
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;

jscheuer1
03-02-2007, 05:29 AM
<!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>

wkenny
03-02-2007, 08:21 AM
Thanks John. Got it working in my form without problems. Thanks again.