Infinite text with jQuery

2

Would it be possible to set using jquery 3.3.1 a standard character that can not be removed to an input? ex: An input has a @ , and only 20 letters and / or numbers can be added after this @ , but @ does not delete. I tried setting using the value="@" of the html. But you can erase it. How do you make sure it is not removable?

    
asked by anonymous 02.08.2018 / 05:11

1 answer

3

You can do this by checking if the first character of the field has @ with event oninput :

campo.oninput = function(){
   if(this.value[0] != "@"){
      this.value = "@"+this.value;
   }
}
<input id="campo" type="text" value="@">

If it does not, it will force the insertion of the character at the beginning of the field. The [0] takes the first character of any string.

    
02.08.2018 / 05:20