/    Sign up×
Community /Pin to ProfileBookmark

error when try 2 replace latin accents

greetings!
I have looked around, found several scripts, read about the string.replace but I still can’t pull it together.
In Bar.html I have a form (BarFrm) and it has several inputs, in the head of the page I have a validating function. In this validating function I want to set the code to replace all occurrences of latin accents the user may had written for its unaccented form:
á –> a
é –> e
í –> i
ó –> o
ú –> u
This because if Insert them to the database without the replace it will not be set as the same letter
[B]á [/B] [I]is set as[/I] [B]é[/B]

here is my function. so far it does validate all but the replace:

[code=html]
<script language=”javascript”>
function trim(str)
{
return str.replace(/^s+|s+$/g,”);
}//trim

function ValBarFrm(){
var CanCar = 0;
var Nombre, Provincia, Canton, Direccion, Caract1, Caract2, Caract3, Caract4, Caract5, Caract6, Caract7, Caract8, Caract9, Caract10, Caract11;

<!– This is the important part. I want Nombre to have its és replaced –>
Nombre = window.document.BaresFrm.Nombre;
Nombre = Nombre.replace(/é/g,”e”);

Provincia = window.document.BaresFrm.Provincia;
Canton = window.document.BaresFrm.Canton;
Caract1 = window.document.BaresFrm.Caract1;
Caract2 = window.document.BaresFrm.Caract2;
Caract3 = window.document.BaresFrm.Caract3;
Caract4 = window.document.BaresFrm.Caract4;
Caract5 = window.document.BaresFrm.Caract5;
Caract6 = window.document.BaresFrm.Caract6;
Caract7 = window.document.BaresFrm.Caract7;
Caract8 = window.document.BaresFrm.Caract8;
Caract9 = window.document.BaresFrm.Caract9;
Caract10 = window.document.BaresFrm.Caract10;
Caract11 = window.document.BaresFrm.Caract11;
Direccion = window.document.BaresFrm.Direccion;
var Enviar = new Boolean(true);

if(trim(Nombre.value) == ”)
{
alert(‘Tienes que poner el nombre del Bar’);
Nombre.focus();
Enviar = false;
}

else if(trim(Provincia.value) == ”)
{
alert(‘Seria muy util que pusieras la provincia’);
Provincia.focus();
Enviar = false;
}

else if(trim(Canton.value) == ”)
{
alert(‘Hace falta el canton, como ejemplo: Escazu, San Pedro’);
Canton.focus();
Enviar = false;
}

else if(trim(Direccion.value) == ”)
{
alert(‘Si no pones la direccion no sabremos como llegar…’);
Direccion.focus();
Enviar = false;
}

if(trim(Caract1.value) != ”)
{
CanCar += 1;

}
if(trim(Caract2.value) != ”)
{
CanCar += 1;

}
if(trim(Caract3.value) != ”)
{
CanCar += 1;

}
if(trim(Caract4.value) != ”)
{
CanCar += 1;

}
if(trim(Caract5.value) != ”)
{
CanCar += 1;

}
if(trim(Caract6.value) != ”)
{
CanCar += 1;

}
if(trim(Caract7.value) != ”)
{
CanCar += 1;

}
if(trim(Caract8.value) != ”)
{
CanCar += 1;

}
if(trim(Caract9.value) != ”)
{
CanCar += 1;

}
if(trim(Caract10.value) != ”)
{
CanCar += 1;

}
if(trim(Caract11.value) != ”)
{
CanCar += 1;
}

if (CanCar < 2){
alert(‘Tienes que poner al menos 2 caracteristicas del lugar’);
if(Caract1.value!=”){Caract2.focus();} else {Caract1.focus();}
Enviar = false;}

return Enviar;

}//Revisar

</script>
[/code]

but the replace after the comments doesnt work.
Help please ?
thanks!

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 20.2005 — Try using unicode escapes

Nombre = Nombre.replace(/[B]u00e9[/B]/g,"e");

here's an online convertor for special characters

http://www.hot-tips.co.uk/useful/unicode_converter.HTML
Copy linkTweet thisAlerts:
@purefanauthorOct 20.2005 — still the same...
Copy linkTweet thisAlerts:
@Orc_ScorcherOct 20.2005 — If by "doesn't work" you mean the name is still submitted with é, that is because you forgot to set the form field to the changed value:

document.BaresFrm.Nombre = Nombre;
Copy linkTweet thisAlerts:
@purefanauthorOct 20.2005 — I assign the new value to the same variable:

[code=html]Nombre = window.document.BaresFrm.Nombre;
Nombre = Nombre.replace(/u00e9/g,"e");[/code]


also with explicit é.

I also tried to do it in the PHP validation page, (JS validation, then PHP validation) using:
[code=php]$Names = str_replace("é","e",$Names);
// And
$Names = strtr($Names,"é","e");[/code]

but it didnt work also. I think the php failure may be due to the server's configuration, because I printed plain é and displayed it wrong:
[code=php]echo 'é';
// output: Ä(C) or something like it
[/code]
Copy linkTweet thisAlerts:
@KorOct 21.2005 — It works for me:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function valid(field) {
field.value = field.value.replace(/u00e9/g,"e");}
</script>
</head>
<body>
<textarea name="text" cols="" rows="" onblur="valid(this)"></textarea>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@purefanauthorOct 21.2005 — ok this is [b]very[/b] f****d up.

Kor: your example is very nice, however even when I did minimal changes it didnt work, nor in Internet Explorer nor in Mozilla Firefox. However I found out something very interesting: after adding the DOCTYPE tagas and the META tags as in your example [U]Internet Explorer[/U] handled correctly the accents, that means that when the input was added to the database it was added 'as-is', with the very same letters the user inputted, with accents.

I tried a Microsoft example using onBlur with just an alert message and worked though...I dont know what could be wrong with the way you are calling the valid function

However it was not the case for [U]Mozilla Firefox[/U]. Having the exact same page Mozilla adds the accents to the database as weird characters.

This is acward I reccon, I dont know what is going on as I have tried many functions...I guess I will have to print a sign saying "DO NOT USE ACCENTS" but that...well Im feeling down for not being able to fix this...
Copy linkTweet thisAlerts:
@KorOct 21.2005 — I see now that this forum's inputs erase the escapes (the backslash) in certain circumstances


Still this code works for me in all browsers (i have coloured in red the missing escape.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta http-equiv="Content-Style-Type" content="text/css">

<meta http-equiv="Content-Script-Type" content="text/javascript">

<script type="text/javascript">

function valid(field) {

field.value = field.value.replace(/[COLOR=Red][/COLOR]u00e9/g,"e");}

</script>

</head>

<body>

<textarea name="text" cols="" rows="" onblur="valid(this)"></textarea>

</body>

</html>



Well, doctype is essential, but the important thing is to specify also the charset you are dealing with.



Well, I have no French characters, but I have tested the character é (as well as I have typed right now here) by using the keystroke Alt-0233 (by the way, the unicode or keystroke equivalency is easy to be found on charmap assistent, if u are using a windows OS).
Copy linkTweet thisAlerts:
@KorOct 21.2005 — The main ideea is simple: when passing a string to a javascript code, you call the javascript interpretor to evaluate the string. The evaluation is always made by an unicode translation, but the automate translation is made, by default, for a limited number of common latin chararcters. To tell the interpretor which kind of another special character you are dealing, you must do the translation "by hand", that means by specify by your self the unicode, otherwise it will be understood as a litterally string.
Copy linkTweet thisAlerts:
@purefanauthorOct 21.2005 — I found a way around!

using PHP and a nice trick I finally got to replace the chars.

http://www.webdeveloper.com/forum/showthread.php?t=82796

Thanks people for paying attention and spending time in my problems!
Copy linkTweet thisAlerts:
@KorOct 21.2005 — I still think strongly that my javascript solution works, but if you have found a server-side one, probably is better, as in fact you need it at a server level (and, unfortunately, some users might disable their client-side languages - javascript).
Copy linkTweet thisAlerts:
@purefanauthorOct 21.2005 — Maybe I implemented your code in the wrong way...

Im just happy I could get through with this and yes, I think it is perhaps better do the validation at a server level for the same reason.

This project is coming along nicely ?
Copy linkTweet thisAlerts:
@herodote92Oct 21.2005 — I have no idea of how this will be displayed on your screens, but I made this simple test to check the French accentuated letters. I saved it as UTF-8, and both FireFox and IE were smart enough to admit hat this was UTF-8 indeed, and display it just like I expected it.

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;French accents&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
var accents = "àâäéèêëîïôöûüçÇ"
var line1 = "";
var line2 = "";
for (var i=0;i&lt;accents.length;i++) {
line1 += accents.charAt(i)+"....";
line2 += accents.charCodeAt(i)+" ";
}
alert(line1+"n"+line2);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


(In case it doens't work: the 'accents' string was: a grave, a circumflex, a trema, e acute, e grave, e circumflex, e trema, i circumflex, i trema, o circumflex, o trema, u circumflex, u trema, c cedilla lowercase, c cedilla uppercase).

As Kor said, 'e acute' is u0233.
×

Success!

Help @purefan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...