×

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 dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

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 Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

Javascript getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

Javascript Re-Password Validation

Example <!DOCTYPE html>   <html>   <head>   <script>   function pass_validation()   {   var firstpassword=document.f1.password1.value;     var secondpassword=document.f1.password2.value;     if(firstpassword==secondpassword){     return true;     }     else{   alert("Password does Not Match");     return false;     }     }    </script>   </head>   <body>   <form name="f1" action="/JavaScript/Index" onsubmit="return pass_validation()">   Password:<input type="password" name="password1" /><br/>   Re-enter :<input type="password" name="password2"/><br/>   <input type="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 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.

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 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 Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

JavaScript p5.js

Introduction to p5.js P5.js is a JavaScript library for creative programming. It is built on Processing, a coding environment for creativity. Processing's major goal is to make it as simple as...

4 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

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...

2 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 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.

Javascript Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.