Forcing form text to be lower-case

前端 未结 11 1193
面向向阳花
面向向阳花 2021-02-02 10:16

How could I force the text in the \"username\" text input to be lower-case regardless of what user types?

11条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 11:17

    setInterval() will run if the user pastes something in

    setInterval(function(){
      let myinput = document.getElementById('myinput');
      //make lowercase
      myinput.value = myinput.value.toString().toLowerCase();
      //remove spaces (if you want)
      myinput.value = myinput.value.toString().replace(' ', '');
      //force specific characters
      myinput.value = myinput.value.toString().replace(/[^a-z0-9\/\-_]/, '');
    }, 10);
    

    because this is a loop function using .replace(), replacing only first occurrence at a time, but still looping all of them, this appears to animate removing spaces on pasted text.

提交回复
热议问题