![](/img/trans.png)
[英]How to restrict user to enter only 2digits only (hours in a day) in textbox using javascript
[英]How to restrict description for 2digits only
我需要驗證我的描述文本區域的文本,其中只包含2個或少於2個數字。它也將包括文本。我試過**regex**
但我沒有結果。我試過像
if(value.match(/^\d{1,2}$/)){
return false;
}else{
return true;
}
手段
有效期:
this is 22 years old
有效期:
this is 222 years old
任何人都可以建議我任何解決方案。謝謝你提前。
沒有任何實施代碼,很難說......
但是你要從正則表達式中刪除^
和$
,它們分別表示字符串的開頭和結尾。
您最好嘗試查找超過2位數的數字,如果找到則返回true:
/\d{3,}/
對於包含3位數字的所有字符串都是如此,因此您知道它不好。
編輯:
if(!value.match(/\d{3,}/)){
return false;
}else{
return true;
}
//Or, as Julian Descottes *almost* pointed out, simply return the value of the function
return !!value.match(/\d{3,}/);
重新讀取OP,我不確定這是否完全是預期的行為,因為它檢查3dBits或更多的NUMBERS,但不計算允許的文本中的位數
I am 24 and my brother is 29
這有四位數,不管是好還是壞?
^\\d{1,2}$
將不匹配任何文本,但它將匹配字符串的開頭和結尾。 因此,它將匹配“34”而不是“1934”或“巴比倫5” 。
你可能想要的是^\\D*\\d{1,2}\\D*$
。 側翼\\ D *的意思是“將0與無限數量的非數字字符匹配”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.