I’ve written a script for a login that creates a username by concatenating first + last name (on blur), removing whitespaces and removing special characters.
Everything works fine in jsFiddle, but not on my site – I get 3 times error ‘$str’ is not defined – so my guess is that the replace function on line 5 + 10 isn’t fired.
Any ideas what’s wrong?
The code:
jQuery().ready(function() {
jQuery('#firstname').blur(function() {
var $login = jQuery('#firstname').val().toLowerCase()+jQuery('#firstname').val().toLowerCase();
$login = $login.replace(/\s/g, "");
$login = replaceChars($login);
jQuery("#login").val($login);
});
jQuery('#lastname').blur(function() {
var $login = jQuery('#firstname').val().toLowerCase()+jQuery('#lastname').val().toLowerCase();
$login = $login.replace(/\s/g, "");
$login = replaceChars ($login);
jQuery("#login").val($login);
});
function replaceChars($str)
{
var charMap = {
é:'e',è:'e',ë:'e',ê:'e',à:'a',
ç:'c',á:'a',ö:'o'
};
var str_array = $str.split('');
for( var i = 0, len = str_array.length; i < len; i++ ) {
str_array[ i ] = charMap[ str_array[ i ] ] || str_array[ i ];
}
$str= str_array.join('');
return($str);
}
});
I just discovered that the problem was caused by the special characters in the replaceChars function – if I replace them with the Javascript entities like \u00EB it works fine…