Search code examples
javascriptjqueryfunctionmaxlength

if input has reached 7 digits, stop the function


So basically I have a button .button, which adds a number to my input #number everytime it's pressed. Now, when I already have 7 digits in my input #number, I want the function to like 'stop working'.

Here is my code (works fine):

  function nrtwo(hello){
            var das = $(hello).html();
            var tempNum = $("#number").val();
            $("#number").val(tempNum + '' + das);
            tempNum = null;
        };

        $(".button").click(function(){
            nrtwo(this);
        });

I was thinking of something like this?

if ($("#number").attr('maxlength') == '7') {
                return false;
            }

Thanks for the help.


Solution

  • Try this .length it is a Number and unblind click event when you reach 7 digits :

    Working jsFiddle

    $(".button").click(function(){
    
     if ($("#number").val().length == 7) {
         $(this).unbind('click');
         return false;
     }else{
         nrtwo(this);
     }
    
    });