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, '&', //the ampersand (&) must be first or else it will be replaced in other replacements
/\u00d1/g, 'Ñ',
/\u00f1/g, 'ñ',
/\u00e1/g, 'á',
/\u00e9/g, 'é',
/\u00ed/g, 'í',
/\u00f3/g, 'ó',
/\u00fa/g, 'ú',
/\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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.