【问题标题】:limit characters in input form with javascript使用javascript限制输入表单中的字符
【发布时间】:2020-09-25 11:41:41
【问题描述】:

我想将输入表单中的字符数限制为 3 到 10。如何使用 Javascript 管理它。我需要使用 Javascript 来完成,这样当用户的输入不符合要求时,我可以将输入字段的边框颜色从黑色更改为红色。这是我的 HTML 代码。我尝试了一些变体,但陷入了混乱。请为我提供简单的解决方案。先谢谢了。

<form action="/index" method="POST">
  <input type="text" id="firstname" name="firstname" placeholder="First name">
  <br>
  <input type="text" id="lastname" name="lastname" placeholder="Last name">
</form>

【问题讨论】:

标签: javascript input


【解决方案1】:

你可以试试这样的;

<input type="text" id="lastname" name="lastname" placeholder="Last name" minlength="3" maxlength = "10">

【讨论】:

    【解决方案2】:

    使用maxlengthminlength

    <form action="/index" method="POST">
      <input type="text" id="firstname" name="firstname" placeholder="First name" minlength="3" maxlength="10">
    </form>
    

    【讨论】:

      猜你喜欢
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多