JS calc with eval for YEARS v2
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
/* voir http://stackoverflow.com/questions/5069464/replace-multiple-strings-at-once */
/* 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 */
#letters {
width: 200px;
}
#numbers {
width: 50px;
}
JavaScript
$('#button').click(function () {
var str = $('#letters').val();
var regx = [/deux mille/gi, /mille/gi, /neuf cent/gi, /huit cent/gi, /sept cent/gi, /six cent/gi, /cinq cent/gi, /quatre cent/gi, /trois cent/gi, /deux cent/gi, /cent/gi, /quatre(-| )vingt(-| )dix/gi, /quatre(-| )vingt/gi, /soixante(-| )dix/gi, /soixante/gi, /cinquante/gi, /quarante/gi, /trente/gi, /vingt/gi, /dix(-| )neuf/gi, /dix(-| )huit/gi, /dix(-| )sept/gi, /seize/gi, /quinze/gi, /quatorze/gi, /treize/gi, /douze/gi, /onze/gi, /dix/gi, /neuf/gi, /huit/gi, /sept/gi, /six/gi, /cinq/gi, /quatre/gi, /trois/gi, /deux/gi, /un/gi, /\+$/igm /* last + */ , /[^-()\d/*+.]/g /* all weird chars */ ];
var repl = ["2000+", "1000+", "900+", "800+", "700+", "600+", "500+", "400+", "300+", "200+", "100+", "90+", "80+", "70+", "60+", "50+", "40+", "30+", "20+", "19+", "18+", "17+", "16+", "15+", "14+", "13+", "12+", "11+", "10+", "9+", "8+", "7+", "6+", "5+", "4+", "3+", "2+", "1+", "" /* remove last + */ , "" /* remove all weird chars */ ];
for (i = 0; i < regx.length; i++) {
str = str.replace(regx[i], repl[i]);
}
var EVAL_IS_DANGEROUS__AVOID_THIS = eval; /* for jslint */
$("#numbers").val(EVAL_IS_DANGEROUS__AVOID_THIS(str));
});