Infinite text with jQuery

Asked

Viewed 42 times

2

Would it be possible to define using jquery 3.3.1 a default character that cannot be removed from an input? ex: An input has a @, and only 20 letters and/or numbers may be added after this @, but the @ can’t delete. I tried to set using the value="@" html. But to delete. How to make it not removable?

  • You cannot add @after?

  • you can, on the php server. But you wanted a front end constraint

  • right. have tried something?

  • On the front end I tried only with atrubuto value, but da para remover quando clica no input

1 answer

2


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

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

If not, it will force the character to be entered at the beginning of the field. O [0] takes the first character of any string.

  • 1

    fast and low!!

  • Obg grandpa! rssss

  • Wow, amazing. Thank you so much.

  • Only one question, because when you put a character before @ one @ and another @ with which it was typed?

  • @Pedrohenrique Good question. Are possibilities. I will analyze here and I’ll give you an answer.

  • See: https://jsfiddle.net/y9cum4de/

  • Great, thank you so much.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.