Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style>
  4. html {
  5. box-sizing: border-box;
  6. }
  7.  
  8. *, *:before, *:after {
  9. box-sizing: inherit;
  10. }
  11. body {
  12. background: #f5f5f5;
  13. color: #333;
  14. font-family: arial, helvetica, sans-serif;
  15. font-size: 32px;
  16. }
  17. h1 {
  18. font-size: 32px;
  19. text-align: center;
  20. }
  21. p {
  22. font-size: 20px;
  23. line-height: 1.5;
  24. margin: 40px auto 0;
  25. max-width: 640px;
  26. }
  27. pre {
  28. background: #eee;
  29. border: 1px solid #ccc;
  30. font-size: 16px;
  31. padding: 20px;
  32. }
  33. form {
  34. margin: 40px auto 0;
  35. }
  36. label {
  37. display: block;
  38. font-size: 24px;
  39. font-weight: bold;
  40. margin-bottom: 10px;
  41. }
  42. input {
  43. border: 0;
  44. color: #333;
  45. font-size: 32px;
  46. margin: 0 0 20px;
  47. padding: .5rem 1rem;
  48. text-align: right;
  49. margin-top: 1px;
  50. }
  51. input:active,
  52. input:focus {
  53. border:0;
  54. outline:none;
  55. }
  56. .input {
  57. -moz-appearance: textfield;
  58. -webkit-appearance: textfield;
  59. background-color: white;
  60. background-color: -moz-field;
  61. border: 1px solid darkgray;
  62. box-shadow: 1px 1px 1px 0 lightgray inset;
  63. height: 58px;
  64. }
  65. button {
  66. background: #fff;
  67. border: 2px solid #333;
  68. border-radius: 5px;
  69. color: #333;
  70. font-size: 16px;
  71. font-weight: bold;
  72. padding: 1rem;
  73. }
  74. button:hover {
  75. background: #333;
  76. border: 2px solid #333;
  77. color: #fff;
  78. }
  79. .main {
  80. background: #fff;
  81. border: 5px solid #ccc;
  82. border-radius: 10px;
  83. margin: 40px auto;
  84. padding: 40px;
  85. width: 80%;
  86. max-width: 700px;
  87. }
  88. </style>
  89. </head>
  90. <body>
  91. <div class="main">
  92. <h1>Auto Formatting Currency</h1>
  93. <form id="form1" method="get" action="#" novalidate>
  94. <label for="currency-field">Enter Amount</label>
  95. <div class="input">
  96. <input type="text" name="currency-field-number" autocomplete="off" inputmode="numeric" pattern="[0-9]*" value="" data-type="currency" placeholder="1.000.000">,
  97. <input type="text" name="currency-field-decimal" autocomplete="off" maxlength="2" inputmode="numeric" pattern="[0-9]*" size="1" value="" placeholder="00">
  98. </div><br/><br/>
  99. <button type="submit">Show me the money</button>
  100. </form>
  101. </div>
  102. <script>
  103. // Check for validate
  104. var form1 = document.querySelector("#form1");
  105.  
  106. form1.addEventListener("submit", function(e){
  107. e.preventDefault(); // stop form from submitting, demo only
  108. alert(this.querySelector("input[name='currency-field-number']").value + ',' + this.querySelector("input[name='currency-field-decimal']").value);
  109. });
  110.  
  111. form1.querySelector("input[name='currency-field-number']").addEventListener("keydown", function(event) {
  112. const acceptedKeys = [8, 9, 13, 35, 36, 37, 38, 39, 40, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 188, 190];
  113. if (acceptedKeys.indexOf(event.keyCode) == -1) {
  114. event.preventDefault();
  115. return false;
  116. }
  117. if (event.keyCode == 188 || event.keyCode == 13 || event.keyCode == 9) { // comma or enter or tab
  118. form1.querySelector("input[name='currency-field-decimal']").focus();
  119. event.preventDefault();
  120. return false;
  121. }
  122. formatCurrency(this, false);
  123. return true;
  124. });
  125.  
  126. form1.querySelector("input[name='currency-field-decimal']").addEventListener("keyup", function(event) {
  127. const acceptedKeys = [8, 9, 35, 36, 37, 38, 39, 40, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
  128. if (acceptedKeys.indexOf(event.keyCode) == -1) {
  129. event.preventDefault();
  130. return false;
  131. }
  132. if (event.keyCode == 8 && this.value.length == 0) {
  133. form1.querySelector("input[name='currency-field-number']").focus();
  134. }
  135. });
  136.  
  137. function formatNumber(n) {
  138. // format number 1000000 to 1.234.567
  139. return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ".");
  140. }
  141.  
  142. function formatCurrency(input, blur) {
  143. // get input value
  144. var input_val = input.value;
  145.  
  146. // don't validate empty input
  147. if (input_val === "") { return; }
  148.  
  149. // original length
  150. var original_len = input_val.length;
  151.  
  152. // initial caret position
  153. var caret_pos = input.selectionStart;
  154.  
  155. // check for decimal
  156. if (input_val.indexOf(",") >= 0) {
  157. // get position of first decimal
  158. // this prevents multiple decimals from
  159. // being entered
  160. var decimal_pos = input_val.indexOf(",");
  161.  
  162. // split number by decimal point
  163. var left_side = input_val.substring(0, decimal_pos);
  164. var right_side = input_val.substring(decimal_pos);
  165.  
  166. // add commas to left side of number
  167. left_side = formatNumber(left_side);
  168.  
  169. // validate right side
  170. right_side = formatNumber(right_side);
  171.  
  172. // On blur make sure 2 numbers after decimal
  173. if (blur) {
  174. right_side += "00";
  175. }
  176.  
  177. // Limit decimal to only 2 digits
  178. right_side = right_side.substring(0, 2);
  179.  
  180. // join number by .
  181. input_val = left_side + "," + right_side;
  182. } else {
  183. // no decimal entered
  184. // add commas to number
  185. // remove all non-digits
  186. input_val = formatNumber(input_val);
  187.  
  188. // final formatting
  189. if (blur) {
  190. input_val += ",00";
  191. }
  192. }
  193.  
  194. // send updated string to input
  195. input.value = input_val;
  196.  
  197. // put caret back in the right position
  198. var updated_len = input_val.length;
  199. caret_pos = updated_len - original_len + caret_pos;
  200. input.setSelectionRange(caret_pos, caret_pos);
  201. }
  202. </script>
  203. </body>
  204. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement