2017-09-19 7 views
-1

私は最大長が4のテキストボックスを持っています。ユーザーが同じ繰り返し番号を入力すると、エラーが発生する必要があります。数字が繰り返されているかどうかをチェック

例: 以下はブロックする必要がありますいくつかの例です:

1111、9999、6666など

をそして、それは受け入れることができます1112年、1222年など、私はjQueryの中で、この条件を期待してい

またはJavaScript。

すべてのヘルプは

コードいただければ幸いです。 私はフォーマット以下のコードを使用したい:

$.validator.addMethod("Pin", function(b) { 
var a = true; 
a = (/^([0-9] ?){4}$/i).test(b); 
return a 
}, ""); 
+3

あなたはこれまで何を試してみましたか? – guradio

+0

あなたの努力とコードを示してください。 –

+0

[数字が順番に並んでいるかどうかをチェックする](https://stackoverflow.com/questions/46296504/check-if-numbers-are-in-sequence) –

答えて

0

を、私はそれがあなた

var oldValue; 
 
$('.limitRepeaters').keypress(function(e){ 
 
\t oldValue= {val : this.value, pos : this.selectionStart }; 
 
}).keyup(function(e){ 
 
\t if (/([^.])\1{3,}/.test(this.value)) { 
 
    \t this.value = oldValue.val; 
 
    this.selectionStart = oldValue.pos; 
 
    alert("Do not repeat") 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input class="limitRepeaters">

助けになると思います
+0

の可能な複製が役立つ場合は、 'check'をクリックしてください:) –

+0

あなたが何をしているのか説明すれば、より良い答えになるでしょう。 – Ivar

+0

あなたの答えをありがとう。 (0)]){4} $/i).test(b); 返り値:返り値: a }、 ""); – user3774781

0

このため、正規表現:

function checkResult(){ 
 
    var value = $('#txtValues').val(); 
 
    var regex= /^([0-9])\1*$/; 
 
    if(regex.test(value)){ 
 
     console.log('Invalid input, '+value); 
 
    }else{ 
 
     console.log('Proper input, '+value); 
 
    } 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id='txtValues' type='text' maxlength='4'/> 
 
<button onclick='checkResult()'>Click me</button>

+0

あなたの正規表現は、任意の組み合わせの4桁の文字列の一致を返しません。正規表現のパターンについては私の答えを見てください。 – Bejasc

+0

@Bejasc質問に「ユーザーが同じ繰り返し番号を入力してエラーを投げる必要がある場合」という質問があります。 – Ivar

+0

@Bejasc質問を最初にお読みください –

0

$("#number").keyup(function() { 
 
    var inp = $("#number").val(); 
 
    if (inp.length == 4) { 
 
    if (inp.charAt(0) == inp.charAt(1) && inp.charAt(0) == inp.charAt(2) && inp.charAt(0) == inp.charAt(3)) { 
 
     console.log("error"); 
 
    } 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" maxlength="4" id="number">

-1

あなたは同じ数字の4を検出するために、この正規表現パターンを使用することができます。

\b(\d)\1+\b 

Regex Tester

0

$(document).ready(function() { 
 
    $("em").addClass("selected"); 
 
    $("#myid").addClass("highlight"); 
 
}); 
 

 

 
function Validate() { 
 
    var text = $("#textbox1").val(); 
 

 

 
    if (!isNaN(parseInt(text))) { 
 
    var number = parseInt(text); 
 

 
    var digits = ("" + number).split(""); 
 

 

 

 
    var firstDigit = digits[0]; 
 

 
    var countSame = 1; 
 

 
    if (digits.length == 4) { 
 
     for (j = 1; j < digits.length; j++) { 
 
     if (firstDigit == digits[j]) { 
 
      countSame++; 
 

 
     } 
 

 
     } 
 
    } 
 

 
    if (countSame == 4) { 
 
     alert("same number occurred 4 times"); 
 
    } 
 

 

 

 
    } 
 

 

 

 

 

 
}
.selected { 
 
    color: red; 
 
} 
 

 
.highlight { 
 
    background: yellow; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<em title="Bold and Brave">This is first paragraph.</em> 
 
<p id="myid">This is second paragraph.</p> 
 

 
<input type='text' name='textBox1' id='textbox1' oninput="Validate()" maxlength="4">

関連する問題