我有一个包含maxlength 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
}, "");
答案 0 :(得分:0)
答案 1 :(得分: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>
&#13;
答案 2 :(得分: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">
答案 3 :(得分: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">
答案 4 :(得分:-1)