/ Published in: jQuery
Expand |
Embed | Plain Text
/* Usage : just give the class name to inputbox from one of these : lDateBox , l DateTime, lDecimalBox , lInteger , lMoney */ jQuery.fn.ForceNumericOnly = function () { return this.each(function () { $(this).keydown(function (e) { var key = e.charCode || e.keyCode || 0; return ( key == 8 || key == 9 || key == 46 || key == 17 || key== 17 || key== 67 || (key >= 37 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)); }) }) }; $(document).ready(function () { $(".lDecimalBox").ForceNumericOnly(); $(".lInteger").ForceNumericOnly(); $(".lDateBox").ForceNumericOnly(); $(".lDateTime").ForceNumericOnly(); $(".lMoney").ForceNumericOnly(); $(".lTinyInt").ForceNumericOnly(); var count = 1; $(".lDateBox").keyup(function (event) { if (event.keyCode == '190') { inp = $(this).val(); len = $(".lDateBox").val().length; $(this).val(inp.substring(0, len - 1)); } if (event.keyCode != '8') { var inp = $(this).val(); var len = $(".lDateBox").val().length; switch (len) { case 2: if (inp.substring(0, 2) <= 31 && !isNaN(inp.substring(0, 2))) { $(this).val(inp + ".") } else $(this).val(""); break; case 5: if (inp.substring(3, 5) <= 12 && !isNaN(inp.substring(3, 5))) $(this).val(inp + "."); else $(this).val(inp.substring(0, 3)); break; case 8: if (inp.substring(6, 8) != 19 && inp.substring(6, 8) != 20) { if (inp.substring(6, 8) < 50 && !isNaN(inp.substring(6, 8))) { $(this).val(inp.substring(0, 6) + "20" + inp.substring(6, 8)); } else { $(this).val(inp.substring(0, 6) + "19" + inp.substring(6, 8)); } } break; } } }); $(".lDateTime").keyup(function (event) { // if (event.keyCode = '190') { // inp = $(this).val(); // len = $(".lDateTime").val().length; // $(this).val(inp.substring(0, len - 1)); // } if (event.keyCode != '8') { var inp = $(this).val(); var len = $(".lDateTime").val().length; if (len == 10) $(this).val(inp.substring(0, 10) + " "); switch (len) { case 2: if (inp.substring(0, 2) > 31 || isNaN(inp.substring(0, 2))) { $(this).val(""); } else $(this).val(inp + "."); break; case 5: if (inp.substring(3, 5) <= 12 && !isNaN(inp.substring(3, 5))) $(this).val(inp + "."); else $(this).val(inp.substring(0, 3)); break; case 8: if (inp.substring(6, 8) != 19 && inp.substring(6, 8) != 20) { if (inp.substring(6, 8) < 50 && !isNaN(inp.substring(6, 8))) { $(this).val(inp.substring(0, 6) + "20" + inp.substring(6, 8) + " "); } else { $(this).val(inp.substring(0, 6) + "19" + inp.substring(6, 8) + " "); } } break; case 13: if (inp.substring(11, 13) <= 23 && inp.substring(11, 13) > 0 && !isNaN(inp.substring(11, 13))) $(this).val(inp.substring(0, 16) + ":"); else { $(this).val(inp.substring(0, 11)); } break; case 16: if (inp.substring(14, 16) < 60 && inp.substring(14, 16) > 0 && !isNaN(inp.substring(14, 16))) $(this).val(inp.substring(0, 16) + ":"); else $(this).val(inp.substring(0, 14)); break; case 19: if (!(inp.substring(17, 19) <= 60 && inp.substring(17, 19) > 0 && !isNaN(inp.substring(17, 19)))) $(this).val(inp.substring(0, 17)); break; } } }); $(".lDecimalBox").keyup(function (event) { if (event.keyCode != '8') { //Backspace not pressed range = $(this).attr("value").split(","); //maximum replacing dot with blank if (range[0].length > 15) { range[0] = range[0].substring(0, 15); } for (i = 0; i < 4; i++) range[0] = range[0].replace(".", ""); lenDecimal = range[0].length; pA = 0; if (lenDecimal >= 3) { pA = lenDecimal % 3; count = 1; a = range[0].substring(0, pA); b = ""; while (lenDecimal - pA >= 3) { if (pA != 0 || count != 1) b += "."; if (isNaN(pA)) { $(this).val(""); break; } b += range[0].substring(pA + (count - 1) * 3, pA + (count * 3)); lenDecimal = lenDecimal - 3; count++; } //If the range array is greater than one if (range.length > 1) { if (range[1].length > 6) { range[1] = range[1].substring(0, 6); } $(this).val(a + b + "," + range[1]); //If the lenght is one then just some of them } else $(this).val(a + b); if ((a.length + b.length) >= 16) { $(this).val(); } inp = $(this).val().toString(); } } }); $(".lInteger").keyup(function (event) { var inp = $(this).val(); var len = $(".lInteger").val().length; if (len == 1 && inp.substring(0, 1) == "-") $(".lInteger").val(""); if (isNaN(inp.substring(0, len)) || inp > 9999) $(this).val(""); }); $(".lTinyInt").keyup(function (event) { var inp = $(this).val(); var len = $(".lTinyInt").val().length; if (inp <= 255 && inp >= -255 && !isNaN(inp)) { //valid } else { $(this).val(""); } }); $(".lMoney").keyup(function (event) { if (event.keyCode != '8') { //Backspace or del not pressed range = $(this).attr("value").split(","); if (range[0].length > 15) { range[0] = range[0].substring(0, 15); } for (i = 0; i < 4; i++) range[0] = range[0].replace(".", ""); lenDecimal = range[0].length; pA = 0; if (lenDecimal >= 3) { pA = lenDecimal % 3; count = 1; a = range[0].substring(0, pA); b = ""; while (lenDecimal - pA >= 3) { if (pA != 0 || count != 1) b += "."; b += range[0].substring(pA + (count - 1) * 3, pA + (count * 3)); lenDecimal = lenDecimal - 3; count++; } //If the range array is greater than one if (range.length > 1) { if (range[1].length > 2) { range[1] = range[1].substring(0, 2); } $(this).val(a + b + "," + range[1]); } else $(this).val(a + b); if ((a.length + b.length) >= 16) { $(this).val(); } } } }); });
You need to login to post a comment.
