--- title: 'HTML 属性: maxlength' slug: Web/HTML/Attributes/maxlength tags: - Attribute - Attributes - Constraint validation translation_of: Web/HTML/Attributes/maxlength --- <p>{{HTMLSidebar}}{{draft}}</p> <p><span class="seoSummary"><strong><code>maxlength</code></strong> 属性は、ユーザーが {{htmlelement('input')}} または {{htmlelement('textarea')}} に入力できる最大文字数を (UTF-16 コード単位で) 定義します。 0 以上の整数値である必要があります。</span> maxlength が指定されなかった場合、または無効な値が指定された場合は、 input や textarea には最大長が設定されません。</p> <p><code>maxlength</code> の値はすべて、 <code>minlength</code> が存在して有効である場合は、その値以上である必要があります。フィールドのテキスト値の長さが maxlength の UTF-16 コード単位の長さを超える場合、入力欄は制約検証に失敗します。制約検証は、ユーザーが値を変更した場合にのみ適用されます。</p> <h3 id="Constraint_validation" name="Constraint_validation">制約検証</h3> <p>ブラウザーは一般的に、 maxlength 属性が許可する以上のテキストをユーザーが入力することを防止しますが、長さが maxlength が許可しているよりも長くなった場合、 {{domxref("ValidityState")}} オブジェクトの読み取り専用の {{domxref("ValidityState.tooLong", "tooLong")}} プロパティが true になります。</p> <h2 id="Examples" name="Examples">例</h2> <pre class="brush: html notranslate"><input type="password" maxlength="4"/> </pre> <p>{{EmbedLiveSample('Examples', '100%', 200)}}</p> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> <th scope="col">状態</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('HTML WHATWG', 'input.html#attr-input-maxlength', 'maxlength attribute')}}</td> <td>{{Spec2('HTML WHATWG')}}</td> </tr> <tr> <td>{{SpecName('HTML5.1', 'input.html#attr-maxlength-accept', 'maxlength attribute')}}</td> <td>{{Spec2('HTML5.1')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <p>{{Compat("html.elements.attribute.maxlength")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li><code><a href="/ja/docs/Web/HTML/Attributes/minlength">minlength</a></code></li> <li><code><a href="/ja/docs/Web/HTML/Attributes/size">size</a></code></li> </ul>