1

I recently received some excellent advice for writing some jquery functionality. I am trying to turn it into a pluggin.
this is the code that I got from stackOverflow@micha at this link

https://stackoverflow.com/a/8820946/729820

$('#DischargeDateTimeMask').keypress(function (e) {
        var regex = ["[0-1]",
                     "[0-2]",
                     ":",
                     "[0-5]",
                     "[0-9]",
                     "(\\s)",
                     "(A|P)",
                     "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
    function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }

I personally attempt to turn it into a pluggin like so...

(function ($) {
$.fn.simpleTimeMask = function () {
    $(this).keypress(function (e) {
        debugger;
        var regex = ["[0-2]",
        "[0-4]",
        ":",
        "[0-6]",
        "[0-9]",
        "(A|P)",
        "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
}


    (function ($) {
        var methods = {
            keyboard: function (a) {
                //THIS
                var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b)
            }
        }
    });

function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }
 })(jQuery);

Notice how I have two functions that attempt to do the same thing. One I think would be a function that would be called by a user of this pluggin, the other would be used internally. Anyway, I got this thing all compiled and I attempted to add it to my project, but as expected, it did not work. What would be your recommendations for building this pluggin?

Community
  • 1
  • 1
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

0 Answers0