×

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute.

The maxLength attribute defines the maximum number of characters allowed in the input field.

NOTE: If the value of maxlength() attribute is not defined, then the default value is taken as 524288 by default.

This method is supported by mostly all the browsers like Google chrome, safari, Microsoft Edge/Internet Explorer, Oracle, Firefox etc.

Syntax:

<input maxlength="number">

Here, the number must be 0 or higher. It can take negative values.

Example:

<!DOCTYPE html>
<html>
<body>


<h3>The following is an example to demonstrate the input maxlength attribute</h3>


<form>
  <label for="tcontent">Enter your text:</label>
  <input type="text" id="tcontent" name="tcontent" maxlength="25">
  <br>
  
</form>


</body>
</html>

Output:

The input filed won’t allow more than 25 characters to be entered in the input field:

Javascript Input Maxlength

Some key points:

  • The length specified in maxlength should be 0 or more than 0.
  • The minlength attribute can be defined to specify the minimum number of characters allowed in the input field for the input field. So, the value of maxlength should always be greater than or equal to the value of minlength (if it is present).
  • Constraint validation will fail in the input if the field’s text length or value is greater than maxlength UTF-16 code units long. Constraint validation comes into purpose when the value is changed by the user.

What is Constraint validation?

The browser generally prevents ay user from entering more text than the maxlength attribute specifies. If by chance, the length of text entered by the user is longer than the maxlength allows, then the read-only tooLong property of a ValidityState object becomes true.

NOTE: If the user wants to find the width of an input filed, the size() property is used.

How is size() property used?

It takes the default value as 20.

Syntax:

To return the size of the field:

textObject.size

To set the size of the field:

textObject.size = “number”

Example:

Here, the example shows both the functionalities of size() property.

<!DOCTYPE html>
<html>
<body>


Enter text here: <input type="text" id="tcontent" size="30">


<p> Select the button to view the value of the size attribute in the input field.</p>


<button onclick="myFunctiondemo()">Click</button>


<p id="displaysize"></p>


<script>
function myFunctiondemo() {
  var x = document.getElementById("tcontent").size;
  document.getElementById("displaysize").innerHTML = x;
}
</script>


</body>
</html>

Output:

Javascript Input Maxlength

What is the difference between size and maxlength in HTML?

The max-length attribute is used to define the number of characters that can be entered in the input field irrespective of how wide the fields appears on the screen. However, the size attribute determines how large the field will appear on the screen.


Related Topics

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

6 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

Javascript Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

1 minute read.

JavaScript setInterval()

On the Window and Worker interfaces, the setInterval() method calls a function or executes a snippet with a specified time delay between each call. This function provides an interval ID that...

4 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.

JavaScript Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

JavaScript redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 minutes read.

Javascript Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

JavaScript setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

3 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.