Limit Input Charcters using HTML CSS and JavaScript with free source code

Social Links

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Character Limit</title>
    <!--Google Font-->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <textarea id="my-text" rows="5" placeholder="Type something here.."></textarea>
        <p id="result"></p>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>
CSS
*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    background-color: #11131e;
}
.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";
    }
});

Post a Comment

Previous Post Next Post