检查数字是否重复 [英] check if numbers are repeating

查看:70
本文介绍了检查数字是否重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,该文本框的最大长度为4,如果用户输入相同的重复数字,则需要抛出错误.

I have a textbox which contains a maxlength of 4 and if the user enters same repeated numbers need to throw an error.

示例: 以下是一些需要阻止的示例:

Examples: Below are few examples which needs to block:

1111、9999、6666等

1111, 9999, 6666 etc

它可以接受1112、1222等

And it can accept 1112, 1222 etc

我期望在Jquery或JavaScript中出现这种情况.

I'm expecting this condition in Jquery or JavaScript.

任何帮助将不胜感激

代码: 我想使用以下格式的代码:

Code: I want to use the code in below format:

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

推荐答案

$("#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">

这篇关于检查数字是否重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆