javascript - Force decimals in html inputs with InputMask html (jqueryl or js)? -


i want force html input following display: 14 mantissa maximun(from 1 14) , effective 3 decimal point, if user not specify value of points after take default 000 example: enter 5 show 5.000 enter 5.2 show 5.200 enter 22222 show 22222.000 display. please help

use .indexof check . character. make sure it's there , check length of substring goes after index. if it's less 3, add zeros.

html

<input type="text" id="input" maxlength="14" value="" /> 

javascript

/* select desired input, , add onchange event listener */ document.queryselector('#input').onchange = function () {     /* if user didn't add dot, add 1 3 zeros */     if (this.value.indexof('.') == -1) this.value += '.000';      /* otherwise, check how many numbers there after dot        , make sure there's @ least 3*/     if (this.value.substring(this.value.indexof('.') + 1).length < 3)         while (this.value.substring(this.value.indexof('.') + 1).length < 3)             this.value += '0'; }; 

live demo


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -