I have a textbox that holds "From" month. (allows values between 1 and 12 only)
How could I prevent users so that when they try to type 3rd character, javascript would prevent users from typing more than 2 digits?
Here is what I have tried so far but I am not able to get desired result(following script does not work at all)
    $("#monthFromTextBox").bind("change", function (event) {
        var regex = /^\d{1,2}$/g;
        var monthFrom = $(this).find(".monthFrom").text();
        var yearFrom = $(this).find(".yearFrom").text();
        var monthTo = $(this).find(".monthTo").text();
        var yearTo = $(this).find(".yearTo").text();
        // Allow only 1 or 2 digits only
        if (regex.test(monthFrom) === false) {
            event.preventDefault();
            // alert("Month From value should have 1 or 2 digits only");
            return false;
        }
    });
                <input type="text" maxlength="2">
                        for the luls... if you really wanted a JS answer..
$("#monthFromTextBox").on('keydown', function() {
    if (this.value.length > 1) {
        return false;
    }
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With