如何使用jquery

时间:2016-02-17 06:08:41

标签: jquery

我有一个手机号码字段。我需要实现验证,如果用户连续输入一个号码5次,它应该警告错误。

例如,如果用户输入类似11111345或22222777的内容,则应提示错误。

2 个答案:

答案 0 :(得分:0)

您可以将全局变量用于此目的。我不知道代码是否有效,但你可能会抓住逻辑。

var window.inputcontrol;
var window.count=0;
$("#yourInput").keyup(function (){

var inputtedNumber;
//get the last character
inputtedNumber = $("#yourInput").val().slice(-1);

if (inputtedNumber===inputcontrol){
//if inputted value is the same holded value 
count= count+1;
} else {
//if inputted value is not the same with holded value, reset counter
inputcontrol=inputtedNumber;
count=0;
}

if (count===5){
alert("your alert");
} 

});

答案 1 :(得分:0)

使用此正则表达式恰好匹配5个连续数字......

/(^|(.)(?!\2))(\d)\3{4}(?!\3)/
相关问题