How To Limit Character In Input Field using JavaScript

Limit Character In Input Field using JavaScript

Limit Character is a common feature in the input box. Whenever we see this type of JavaScript Character Limit in the input box of any type of website. The biggest example of this is Twitter.

Here I have created a limit character input box. Html, CSS, and javascript are used to create. Although you can create Limit Character using the max length attribute of HTML.

Limit Character In Input Field

However, even if you use the maxlength attribute, you will not be able to input more than that limit in the input box. But in this case, you can input as much as you want. When your input character exceeds the limit, a kind of error will appear.

Here I have used the Character's limit of 50. When you input more than 50 characters, the color of the border will change.


See the Pen Untitled by Shantanu Jana (@shantanu-jana) on CodePen.


As you can see above, this is a simple Javascript Limit Characters. A box has been created on the webpage. An input box has been created in that box using textarea. 

It also has a display where the characters will count. Normally the color of the border around the input box is green. When you input more characters than the limit, the color of the border will be red. The color of the counted characters in the display will be red.

How to Create Limit Input Characters in JavaScript

Create HTML and CSS files to make it. Here I have given all the source codes. Copy those codes and add them to your file.

Limit Character HTML code

The following codes are HTML codes that have been used to add basic structure and information to this project.


<!DOCTYPE html>
<html lang="en">
<head>
    <title>Limit Characters In Input Field Javascript</title>
    <!--CSS file-->
    <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>

</body>
</html>

 Limit Character Input CSS Code

The following code is CSS which has designed Limit Character In Input Field.


*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    background-color: #a9d1ea;
}
.container{
    background-color: #ffffff;
    width: 450px;
    min-height: 250px;
    padding: 30px 20px;
    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%;
    min-height: 200px;
    resize: none;
    padding: 20px;
    color: #202020;
    border: 3px solid #3ba51f;
    border-radius: 5px;
    outline: none;
}
p{
    width: 100%;
    text-align: right;
    margin-top: -30px;
    padding-right: 10px;
    color: #737373;
}

Character limit JavaScript Code

The following code is JavaScript by which Character Limit is activated.


var myText = document.getElementById("my-text");
var result = document.getElementById("result");
var limit = 50;
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 = "#31821b";
        result.style.color = "#31821b";
    }
});

Hopefully, you have been able to create this JavaScript Limit Character In Input using the above code. If there is any problem then use the download button below. If there is any problem in making this textarea limit characters, please comment.







Comments

Popular posts from this blog

Simple Day Night Toggle Button Using HTML and CSS

Splash Image Mask Using HTML and CSS (Free Code)

Simple RGB Color Generator Using JavaScript & CSS