【问题标题】:Validate the textbox for length验证文本框的长度
【发布时间】:2015-09-22 16:41:13
【问题描述】:
<input type="text" placeholder="Title" onkeyup="CheckLength(this.value);"/>

在 JavaScript 文件中,

function CheckLength(currentValue)
{
   if(currentValue.length > 6)
      alert("Only 6 letters are allowed, please follow the rule!");
}

我想要的是在 6 个字符之后,无论用户输入什么都不应该在文本框中显示,并且应该显示一个警报..你能帮我吗?

【问题讨论】:

  • 你想要一个 jQuery 解决方案吗?一个angularjs解决方案?原生 javascript 解决方案?
  • 我想要原生 js 或 angular js

标签: javascript jquery html angularjs


【解决方案1】:

有一个叫做 maxlength 的属性。

maxlength 属性指定元素中允许的最大字符数。

<input type="text" placeholder="Title" maxlength="6"/>

【讨论】:

    【解决方案2】:

    function CheckLength(currentValue)
    {
       if(currentValue.length > 5)
    	  return false;
    }
    &lt;input type="text" onkeydown="return CheckLength(this.value);"/&gt;

    你也可以添加属性 maxlength 到输入元素来限制字符的数量,比如

    <input type="text" maxlength="6"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      相关资源
      最近更新 更多