2015-09-22 51 views
-3
<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個字符,無論用戶在文本框中鍵入應不可見的,應該顯示一個警告..你能幫我嗎?

+0

你想一個jQuery的解決方案是多少?一個angularjs解決方案?原生javascript解決方案? – Magus

+0

我想要原生js或角js – varunaer

回答

5

有一種叫maxlength屬性。

maxlength屬性指定元素允許的最大字符數。

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

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

您也可以屬性最大長度添加到輸入要素制約的性格像

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