Converting accented characters for form fields as you type

Converting accented characters for form fields as you type

OK, it appears as though some users would like to convert accented characters on at least one field other than the SEF title field, so after an hour of putting together a Polish specific function, a light went off in my head, I can just re-use most of the code within the genSEF function from the default admin.js, and it should cover other accented characters as well... at least as well as the default sNews does.

This javascript function will convert on every keyup event. 95% of this code comes from the sNews default admin.js, I just put the accented character codes and ASCII equivalents into arrays and stuck them in a loop. And I removed the bit which converts spaces to hyphens, now it just collapses excessive whitespace. Obviously it will accept commas as well.

Try it out, type some accented characters below to convert:

To use this to convert your META tags field, or any field for that matter really, simply copy the javascript function below to your admin.js file, just above the closing //]]> bit, or save it as strip_accents.js and call it within your document head;

function strip_accents(str) {
    var out=str.toLowerCase();
    var accented=[
    /[\xc0-\xc5\xe0-\xe5\u0100-\u0105\u0386\u0391\u03ac\u03b1\u0410\u0430\u05d0]/g,
    /[\xc8-\xcb\xe8-\xeb\u0116-\u011b\u0112\u0113\u0388\u0395\u03ad\u03b5\u042d\u044d]/g,
    /[\xa1\xcc-\xcf\xec-\xef\u0128-\u012b\u012e-\u0132\u013a\u0389\u038a\u0390\u0397\u0399\u03aa\u03ae\u03af\u03b7\u03b9\u03ca\u0418\u0438]/g,
    /[\xd2-\xd6\xd8\xf0\xf2-\xf6\xf8\u014d\u014c\u0150\u0151\u038c\u038f\u039f\u03a9\u03bf\u03c9\u03cc\u03ce\u041e\u043e]/g,
    /[\xb5\xd9-\xdc\xf9-\xfc\u0171\u0173\u0168-\u0170\u0423\u0443]/g,
    /[\u0392\u03b2\u05d1]/g,
    /[\xc7\xe7\u0106-\u010d\u0147\u0148\u05da\u05db]/g,
    /[\xd0\u010e-\u0111\u0394\u03b4\u05d3]/g,
    /[\u03a6\u03c6]/g,
    /[\u011c-\u0123\u0393\u03b3\u05d2]/g,
    /[\u0124-\u0127\u05d4]/g,
    /[\u0134\u0135]/g,
    /[\u0136\u0137\u039a\u03ba\u05d7\u05e7]/g,
    /[\u0139-\u013e\u0141\u0142\u039b\u03bb\u05dc]/g,
    /[\u039c\u03bc\u05dd\u05de]/g,
    /[\xd1\xf1\u0143-\u0148\u039d\u03bd\u05df\u05e0]/g,
    /[\u0154-\u0159\u03a1\u03c1\u05e8]/g,
    /[\u03a0\u03c0\u05e3\u05e4]/g,
    /[\x8a\x9a\xdf\u015a-\u0161\u03a3\u03c2\u03c3\u05e1]/g,
    /[\u0162-\u0167\u021a\u021b\u03a4\u03c4\u05d8\u05ea]/g,
    /[\u05d5]/g,
    /[\u03be\u039e]/g,
    /[\x9f\xdd\xfd\xff\u038e\u03a5\u03ab\u03b0\u03c5\u03cb\u03cd\u05d9]/g,
    /[\x9e\u0179-\u017e\u0396\u03b6\u05d6]/g,
    /[\u05e2]/g,
    /[\xc6\xe6]/g,
    /[\u03a7\u03c7]/g,
    /[\u039e\u03be\u0152\u0153]/g,
    /[\xde\xfe\u0398\u03b8]/g,
    /[\u05e5\u05e6]/g,
    /[\u03c8\u03a8\u0398\u03b8]/g,
    /[\u05e9]/g,
    /[\xdf]/g,
    /[^a-z 0-9,]+/g,
    /\s+/g];
    var stripped=['a','e','i','o','u','b','c','d','f','g','h','j','k','l','m','n','r','p','s','t','v','x','y','z','aa','ae','ch','ce','th','ts','ps','sh','sz','',' '];
    for(var i=0; i<accented.length; i++)
        out=out.replace(accented[i],stripped[i]);
        return out;
}

Now find the field you want to convert, in this case the "Keywords META Tag (keywords separated by comma):" part, BACK-UP your snews.php file and look for

echo html_input('text', 'keywords_meta', 'km', $frm_meta_key, l('keywords_meta'), '', '', '', '', '', '', '', '', '', '');

then add the strip_accents call

echo html_input('text', 'keywords_meta', 'km', $frm_meta_key, l('keywords_meta'), '', 'onkeyup="this.value=strip_accents(this.value)"', '', '', '', '', '', '', '', '');

That's it, now when you're filling out the meta tags, it should strip any accents. You can do this to any form field, simply by adding the highlighted code onkeyup="this.value=strip_accents(this.value)" to the field.

Tags

 

You might like

Comments

No comments have been posted to this page.

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top