JavaScript: transform a string to uppercase without accented vowel

<input id=”firstName” onblur=”checkFirstName();” />

function correct(stringToCorrect) {
    var correctedString = stringToCorrect.toUpperCase();
    correctedString = correctedString.replace(/[ÁÀÄÂ]/, “A”);
    correctedString = correctedString.replace(/[ÉÈËÊ]/, “E”);
    correctedString = correctedString.replace(/[ÍÌÏÎ]/, “I”);
    correctedString = correctedString.replace(/[ÓÒÖÔ]/, “O”);
    correctedString = correctedString.replace(/[ÚÙÜÛ]/, “U”);
    return correctedString;
}
  
function checkFirstName() {
    document.forms[0].firstName.value = correct(document.forms[0].firstName.value);
    // Then do stuff with the corrected string
}


This can be declined as follows:


<input id=”firstName” onblur=”correct_bis(this);” />


function correct_bis(myField) {
    var correctedString = myField.value.toUpperCase();
    correctedString = correctedString.replace(/[ÁÀÄÂ]/, “A”);
    correctedString = correctedString.replace(/[ÉÈËÊ]/, “E”);
    correctedString = correctedString.replace(/[ÍÌÏÎ]/, “I”);
    correctedString = correctedString.replace(/[ÓÒÖÔ]/, “O”);
    myField.value = correctedString.replace(/[ÚÙÜÛ]/, “U”);
}

Advertisements

One thought on “JavaScript: transform a string to uppercase without accented vowel

  1. unknown says:

    it doesn’t work if there are more than one accented vowel in the word!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: