Home

minlengthmaxlength

Minlengthmaxlength refers to the common practice of constraining the length of user input by specifying minimum and maximum character counts. It is typically implemented as two related constraints: minlength and maxlength.

In HTML, minlength sets the smallest number of characters allowed, while maxlength sets the largest. For example,

In addition to HTML, JavaScript can be used to validate input length dynamically, for instance by checking

Important considerations include the distinction between text-like inputs and numeric inputs: minlength/maxlength apply to strings, whereas

Overall, minlength and maxlength help prevent invalid submissions and guide users toward acceptable input ranges, but

an
input
like
<input
type="text"
minlength="3"
maxlength="12"
/>
requires
at
least
three
but
no
more
than
twelve
characters.
Browsers
often
perform
client-side
validation
based
on
these
attributes,
especially
when
the
field
is
required,
but
behavior
can
vary
across
environments.
Regardless,
server-side
validation
should
also
enforce
the
same
limits.
value.length
against
defined
bounds
and
providing
immediate
feedback
to
the
user.
Many
validation
schemes
also
combine
these
checks
with
regex
patterns
to
enforce
additional
formats,
such
as
alphanumeric
constraints
or
special
character
rules.
numeric
fields
typically
use
min
and
max
to
constrain
value
rather
than
length.
Empty
input
handling
is
governed
by
the
required
attribute;
minlength
does
not
apply
to
an
empty
value
unless
the
field
is
required.
When
dealing
with
multilingual
text
or
emoji,
length
measured
in
code
units
may
differ
from
perceived
characters,
so
language-aware
counting
or
grapheme-aware
libraries
may
be
needed
for
robust
validation.
should
be
complemented
with
server-side
checks
and
accessible
error
messaging.