HTML
< Div class = "COL-md-4" > < div class = "form-has-Feedback Group Row" > < div class = "COL-md-6 orderMsgRight" > < label > is expected of our investment amount (million ): </ label > </ div > < div class = "COL-MD-orderMsgLeft. 6" > < INPUT name = "investBudget" AutoComplete = "OFF" class = "NUM" placeholder = "Please two decimal places." onkeyup="clearNoNum(this)" type="text" id="investbudget" th:value="${estimated_cost}"> </div> </div> </div>
js
1 // the control input only two decimal 2 function clearNoNum (obj) { . 3 obj.value obj.value.replace = (/ [. ^ \ D] / G, ""); // clear the "number" and . "" other than characters . 4 obj.value obj.value.replace = (/ \ {2,} / G,. "."); // keep only the first remove excess. . 5 obj.value = obj. . value.replace ( ".", "$ # $") Replace (/\./ G, "") .replace ( "$ # $", "." ); . 6 obj.value = obj.value.replace (/^(\-)*(\d+)\.(\d\d).*$/, '$ 2. $ $. 3. 1'); // enter only two decimal places . 7 IF (obj.value.indexOf ( ".") <0 && obj.value ! = "") { // above has been filtered, the control here that if no decimal point, the first 01, 02 can not be similar to the amount . 8 obj.value =parseFloat (obj.value); . 9 } 10 } . 11 // then check again loses focus 12 is $ ( "NUM.") Blur (. function () { 13 is var VL = the this .Value; 14 var REG = new new the RegExp ( "^ [0-9] + ([0-9] {2}) $.?" ); 15 var Boo = reg.test (VL); 16 IF (Boo) { . 17 the this .Value = Number The ( the this .Value) .toFixed (2 ); 18 is } the else { . 19 $ ( the this ) .val ( "" ); 20 } 21 22 });