![](/img/trans.png)
[英]Regex to prevent the same character from appearing more than 3 times in a row
[英]How to prevent the user from repeating a character more than three times in the same word using jquery
如何防止用戶在同一個單詞中重復三次以上的角色,如下所示:
“hiii”它可能,但是
“hiiii”文本框中沒有可能的類型,指示符已停止。
我想像這個網站添加廣告看文本框:الإعلان*
你能幫助我嗎?
我試過這段代碼:
<input name="text1" type="text" class="mult">
var mult = false,prev = 0;
$('.mult').keydown(function(e) {
if (!mult) {
mult = true;
prev = e.which;
setTimeout(function() {
mult = false;
}, 10000)
}
else if (prev != e.which) {
mult = false;
}
else {
return false;
}
});
但這可以防止重復字符超時一次。 我想在沒有時間的情況下防止用戶在同一個單詞中重復三次以上的字符。 我希望當用戶鍵入文本並且他想在一個單詞中重復該字符時指示符停止寫入。
不需要使用jQuery,您可以使用正則表達式來測試字符串。
if (/(.)\1{3,}/.test(str)) {
console.log('Not possible');
} else {
console.log('possible');
}
對於jQuery,您可以使用:
$('. mult').keypress(function(){
if (/(.)\1{2,}/.test($(this).val())) {
return false;
}
})
這里作為工作示例:
$('.limitRepeaters').keypress(function(){ if (/(.)\\1{2,}/.test($(this).val())) { return false; } })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="limitRepeaters">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.