Limit Character In Input Field

HTML
<div class="container">
    <textarea id="my-text" rows="5" placeholder="Type something here..."></textarea>
    <p id="result"></p>
</div>
CSS
*,
*:before,
*:after{
    padding:0;
    margin:0;
    box-sizing:border-box;
}
body{
    background-color:#5d54b3;
}
.container{
    background-color:#ffffff;
    width:450px;
    padding:50px 30px;
    position:absolute;
    transform:translate(-50%,-50%);
    top:50%;
    left:50%;
    border-radius:8px;
    box-shadow:0 20px 25px rgba(0,0,0,0.25);
}
.container *{
    font-family:"Poppins",sans-serif;
    font-size:16px;
}
textarea{
    display:block;
    width:100%;
    resize:none;
    padding:20px;
    color:#202020;
    border:1.5px solid #b2b2b2;
    border-radius:5px;
    outline:none;
}
p{
    width:100%;
    text-align:right;
    margin-top:15px;
    color:#737373;
}
JS
var myText = document.getElementById("my-text");
var result = document.getElementById("result");
var limit = 60;
result.textContent = 0 + "/" + limit;

myText.addEventListener("input",function(){
    var textLength = myText.value.length;
    result.textContent = textLength + "/" + limit;
    
    if(textLength > limit){
        myText.style.borderColor = "#ff2851";
        result.style.color = "#ff2851";
    }else{
        myText.style.borderColor = "#b2b2b2";
        result.style.color = "#737373";
    }
});

Autor: Leonard

Publicado: 21/01/2023

Compartir Elemento Web

Donar a Compositu
Otras formas de ayudar
  • Compártelo en tus redes sociales.
  • Recomienda los elementos.
  • Regístrate Aquí
  • Deja tu comentario agradeciendo el aporte.

Descarga el código completo del Elemento Web y archivos necesarios (imágenes, librerias, plugins, frameword, etc)

Deja tu comentario