JS eval Chiffres romains v1
avec boucle for
by toubia95
HTML
<input type="text" name="letters" id="letters" value="mille neuf cent soixante douze" placeholder="Année en toutes lettres">
<button type="button" id="button">=></button>
<input type="text" name="numbers" id="numbers">
CSS
/*
Penser à 7bre 8bre 9bre Xbre,
deux-vingt, trois-vingt, cinq-vingt, six-vingt
septante, octante, nonante, voir http://monsu.desiderio.free.fr/curiosites/septante.html
ûttante, oitante, huitante, vinz, quinze-vingt, onze cent, dix-neuf cent, (1100->1900)
*/
/* voir http://stackoverflow.com/questions/5069464/replace-multiple-strings-at-once */
/* http://stackoverflow.com/questions/2064047/javascript-replace-globally-with-array */
/* voir http://www.regexr.com/ et http://www.w3schools.com/tags/att_input_pattern.asp */
/* voir http://stackoverflow.com/questions/13147289/jslint-eval-is-evil-alternatives */
/* voir http://www.miakinen.net/vrac/nombres */
#letters {
width: 200px;
}
#numbers {
width: 50px;
}
JavaScript
$('#button').click(function () {
$("#numbers").val(yearRom($('#letters').val()));
});
// Version 3
var yearRom = function (str) {
var array = {
"XIII": "13",
"VIII": "8",
"XIV": "14",
"XII": "12",
"VII": "7",
"III": "3",
"IX": "9",
"IV": "4",
"XI": "11",
"VI": "6",
"II": "2",
"X": "10",
"V": "5",
"I": "1",
"\\+$": "",
"[^-()\\d/*+.]": ""
};
for (var val in array) {
str = str.replace(new RegExp(val, "gi"), array[val]);
}
//var EVAL_IS_DANGEROUS__AVOID_THIS = eval; /* for jslint */
//str = EVAL_IS_DANGEROUS__AVOID_THIS(str);
return str;
};