Hi, I need some help how can i add space if i will input numbers in textfield,I want that if the user will input 888 it will add space directly after the last 8 then if he will continue to add 4 digits 5555 it will add space after the last digit 5,can you help me please how can i achieve this.

my code is not working.

Code:
 $(function(){
             $('#txtno').keyup(function(e){
                 e.preventDefault();
                var counter = 0;
                var dgt = $(this).val();
                  if(counter == '3'){
                    
                    dgt = $(this).val() + " ";
                  }else if(counter == '7'){
                     dgt = $(this).val() + " ";
                  }
                  else{
                      dgt = $(this).val();
                  }

                $(this).val(dgt);
                counter++;
              });
           });

Thank you in advance.