Codeigniter 4 call database inside javascript

I am new to codeigniter 4, and i am using javascript to display the error message using keyup function. The user should register unique username. how can i check the database if username is already taken?

note: without pressing the sign up button(I already try “rules”, but it is need to click the button). the error must display as long as he type his username. This is my EXAMPLE in matching password, and it is already done. myproblem now is checking the database username.

**myrepassword.onkeyup = function() {
  if(myrepassword.value.match(myInput.value)) {  
    repassword.classList.remove("invalid");
    repassword.classList.add("valid");
    document.getElementById("message_repsw").style.display = "none";
  } else {
    repassword.classList.remove("valid");
    repassword.classList.add("invalid");
    document.getElementById("message_repsw").style.display = "block";
    document.getElementById("repassword").innerHTML = "Password does'nt match"
  }
  
}**