连续多个相邻字符校验

最近有一新的功能需求,需要校验密码不能输入>=3个连续相邻的字母或数字,第一反应去网上找正则,翻来找去没有匹配的正则,有一种笨方法是将所有字母数字列举出来,但是代码终究不优美。于是想到了ASCII码去对比来解决,如下

function isKeyBoardContinuousChar(str) {
 var lowStr = str.toLowerCase()  //不区分大小写
  if(lowStr.length>2){  // 连续三位以上
    for(let i=1;i<=str.length-1;i++){
      let firstIndex = lowStr.charCodeAt(i-1)
      let secondIndex = lowStr.charCodeAt(i)
      let thirdIndex = lowStr.charCodeAt(++i)
      i--
      if((firstIndex+1 == secondIndex && secondIndex+1 == thirdIndex) || (firstIndex-1 == secondIndex && secondIndex-1 == thirdIndex)){
        return true
      }
    }
  }
  return false
}

 

posted @ 2022-08-19 16:14  崔崔崔呢  阅读(105)  评论(0)    收藏  举报