Posted By

caglardursun on 04/27/11


Tagged

date format decimal DateTime a in turkish for DDMMYYYY hhmmss


Versions (?)

Filtered InputBox with KeyUp Events


 / Published in: jQuery
 

  1. /*
  2.  
  3. Usage : just give the class name to inputbox from one of these : lDateBox , l DateTime, lDecimalBox , lInteger , lMoney
  4.  
  5. */
  6.  
  7. jQuery.fn.ForceNumericOnly =
  8. function () {
  9. return this.each(function () {
  10. $(this).keydown(function (e) {
  11. var key = e.charCode || e.keyCode || 0;
  12. return (
  13. key == 8 ||
  14. key == 9 ||
  15. key == 46 ||
  16. key == 17 ||
  17. key== 17 ||
  18. key== 67 ||
  19. (key >= 37 && key <= 40) ||
  20. (key >= 48 && key <= 57) ||
  21. (key >= 96 && key <= 105));
  22. })
  23. })
  24. };
  25.  
  26. $(document).ready(function () {
  27. $(".lDecimalBox").ForceNumericOnly();
  28. $(".lInteger").ForceNumericOnly();
  29. $(".lDateBox").ForceNumericOnly();
  30. $(".lDateTime").ForceNumericOnly();
  31. $(".lMoney").ForceNumericOnly();
  32. $(".lTinyInt").ForceNumericOnly();
  33.  
  34. var count = 1;
  35. $(".lDateBox").keyup(function (event) {
  36.  
  37. if (event.keyCode == '190')
  38. {
  39.  
  40. inp = $(this).val();
  41. len = $(".lDateBox").val().length;
  42. $(this).val(inp.substring(0, len - 1));
  43. }
  44.  
  45. if (event.keyCode != '8') {
  46. var inp = $(this).val();
  47. var len = $(".lDateBox").val().length;
  48.  
  49. switch (len) {
  50. case 2:
  51. if (inp.substring(0, 2) <= 31 && !isNaN(inp.substring(0, 2)))
  52. {
  53. $(this).val(inp + ".")
  54. } else
  55. $(this).val("");
  56. break;
  57. case 5:
  58. if (inp.substring(3, 5) <= 12 && !isNaN(inp.substring(3, 5)))
  59. $(this).val(inp + ".");
  60. else
  61. $(this).val(inp.substring(0, 3));
  62. break;
  63.  
  64. case 8:
  65. if (inp.substring(6, 8) != 19 && inp.substring(6, 8) != 20) {
  66. if (inp.substring(6, 8) < 50 && !isNaN(inp.substring(6, 8))) {
  67. $(this).val(inp.substring(0, 6) + "20" + inp.substring(6, 8));
  68.  
  69. }
  70. else {
  71. $(this).val(inp.substring(0, 6) + "19" + inp.substring(6, 8));
  72. }
  73. }
  74. break;
  75. }
  76.  
  77. }
  78. });
  79.  
  80. $(".lDateTime").keyup(function (event) {
  81.  
  82. // if (event.keyCode = '190') {
  83.  
  84. // inp = $(this).val();
  85. // len = $(".lDateTime").val().length;
  86. // $(this).val(inp.substring(0, len - 1));
  87. // }
  88.  
  89. if (event.keyCode != '8') {
  90. var inp = $(this).val();
  91. var len = $(".lDateTime").val().length;
  92.  
  93. if (len == 10)
  94. $(this).val(inp.substring(0, 10) + " ");
  95.  
  96. switch (len) {
  97. case 2:
  98.  
  99. if (inp.substring(0, 2) > 31 || isNaN(inp.substring(0, 2))) {
  100. $(this).val("");
  101. } else
  102. $(this).val(inp + ".");
  103.  
  104. break;
  105. case 5:
  106.  
  107. if (inp.substring(3, 5) <= 12 && !isNaN(inp.substring(3, 5)))
  108. $(this).val(inp + ".");
  109. else
  110. $(this).val(inp.substring(0, 3));
  111. break;
  112. case 8:
  113.  
  114. if (inp.substring(6, 8) != 19 && inp.substring(6, 8) != 20) {
  115. if (inp.substring(6, 8) < 50 && !isNaN(inp.substring(6, 8))) {
  116. $(this).val(inp.substring(0, 6) + "20" + inp.substring(6, 8) + " ");
  117.  
  118. }
  119. else {
  120. $(this).val(inp.substring(0, 6) + "19" + inp.substring(6, 8) + " ");
  121. }
  122. }
  123. break;
  124. case 13:
  125.  
  126.  
  127. if (inp.substring(11, 13) <= 23 && inp.substring(11, 13) > 0 && !isNaN(inp.substring(11, 13)))
  128. $(this).val(inp.substring(0, 16) + ":");
  129. else {
  130. $(this).val(inp.substring(0, 11));
  131. }
  132. break;
  133. case 16:
  134.  
  135. if (inp.substring(14, 16) < 60 && inp.substring(14, 16) > 0 && !isNaN(inp.substring(14, 16)))
  136. $(this).val(inp.substring(0, 16) + ":");
  137. else
  138. $(this).val(inp.substring(0, 14));
  139. break;
  140. case 19:
  141. if (!(inp.substring(17, 19) <= 60 && inp.substring(17, 19) > 0 && !isNaN(inp.substring(17, 19))))
  142. $(this).val(inp.substring(0, 17));
  143. break;
  144. }
  145. }
  146.  
  147.  
  148. });
  149.  
  150.  
  151. $(".lDecimalBox").keyup(function (event) {
  152.  
  153. if (event.keyCode != '8') {
  154. //Backspace not pressed
  155.  
  156. range = $(this).attr("value").split(",");
  157. //maximum replacing dot with blank
  158.  
  159. if (range[0].length > 15) {
  160. range[0] = range[0].substring(0, 15);
  161. }
  162.  
  163. for (i = 0; i < 4; i++)
  164. range[0] = range[0].replace(".", "");
  165.  
  166. lenDecimal = range[0].length;
  167.  
  168. pA = 0;
  169.  
  170. if (lenDecimal >= 3) {
  171. pA = lenDecimal % 3;
  172.  
  173. count = 1;
  174. a = range[0].substring(0, pA);
  175. b = "";
  176.  
  177. while (lenDecimal - pA >= 3) {
  178. if (pA != 0 || count != 1)
  179. b += ".";
  180.  
  181. if (isNaN(pA)) {
  182. $(this).val("");
  183. break;
  184. }
  185.  
  186. b += range[0].substring(pA + (count - 1) * 3, pA + (count * 3));
  187. lenDecimal = lenDecimal - 3;
  188. count++;
  189. }
  190.  
  191. //If the range array is greater than one
  192. if (range.length > 1) {
  193. if (range[1].length > 6) {
  194. range[1] = range[1].substring(0, 6);
  195. }
  196. $(this).val(a + b + "," + range[1]);
  197.  
  198. //If the lenght is one then just some of them
  199. } else
  200. $(this).val(a + b);
  201.  
  202. if ((a.length + b.length) >= 16) {
  203. $(this).val();
  204. }
  205.  
  206. inp = $(this).val().toString();
  207. }
  208. }
  209.  
  210. });
  211.  
  212.  
  213. $(".lInteger").keyup(function (event) {
  214. var inp = $(this).val();
  215. var len = $(".lInteger").val().length;
  216. if (len == 1 && inp.substring(0, 1) == "-")
  217. $(".lInteger").val("");
  218. if (isNaN(inp.substring(0, len)) || inp > 9999)
  219. $(this).val("");
  220. });
  221.  
  222. $(".lTinyInt").keyup(function (event) {
  223. var inp = $(this).val();
  224. var len = $(".lTinyInt").val().length;
  225. if (inp <= 255 && inp >= -255 && !isNaN(inp)) {
  226. //valid
  227. } else {
  228. $(this).val("");
  229. }
  230. });
  231.  
  232. $(".lMoney").keyup(function (event) {
  233. if (event.keyCode != '8') {
  234. //Backspace or del not pressed
  235.  
  236. range = $(this).attr("value").split(",");
  237.  
  238.  
  239. if (range[0].length > 15) {
  240. range[0] = range[0].substring(0, 15);
  241. }
  242. for (i = 0; i < 4; i++)
  243. range[0] = range[0].replace(".", "");
  244.  
  245. lenDecimal = range[0].length;
  246.  
  247.  
  248. pA = 0;
  249.  
  250. if (lenDecimal >= 3) {
  251. pA = lenDecimal % 3;
  252.  
  253. count = 1;
  254. a = range[0].substring(0, pA);
  255. b = "";
  256.  
  257. while (lenDecimal - pA >= 3) {
  258. if (pA != 0 || count != 1)
  259. b += ".";
  260. b += range[0].substring(pA + (count - 1) * 3, pA + (count * 3));
  261. lenDecimal = lenDecimal - 3;
  262. count++;
  263. }
  264.  
  265. //If the range array is greater than one
  266. if (range.length > 1) {
  267. if (range[1].length > 2) {
  268. range[1] = range[1].substring(0, 2);
  269. }
  270. $(this).val(a + b + "," + range[1]);
  271.  
  272. } else
  273. $(this).val(a + b);
  274.  
  275. if ((a.length + b.length) >= 16) {
  276. $(this).val();
  277. }
  278.  
  279. }
  280.  
  281. }
  282.  
  283.  
  284. });
  285.  
  286. });

Report this snippet  

You need to login to post a comment.