序号的正则表达式,例如12345或456789 [英] Regular expression for sequential numbers like 12345 or 456789

查看:46
本文介绍了序号的正则表达式,例如12345或456789的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要防止在文本框中输入 5 个数字序号,例如 12345 45678 等.

I need to prevent entering 5 digit sequential numbers like 12345, 45678 etc. in a text box.

我尝试使用以下正则表达式,但不起作用

I had tried with the following regular expression, but it is not working

var regex = /^\(?[0-9]{3}(\-|\)) ?[0-9]{3}-[0-9]{4}$/;

推荐答案

对于此类任务,最好使用基于非正则表达式的方法.您可以使用 indexOf 轻松地做到这一点.这些模式的正则表达式变得非常复杂且难以阅读.

It is better to use non regular expression based approach for this type of tasks. You can do this easily using indexOf. Regular expressions for these patterns become really complicated and un-readable.

var pattern = '0123456789012345789' //to match circular sequence as well.
if (pattern.indexOf(input) == -1) 
  console.log('good input')
else
  console.log('bad input')

这篇关于序号的正则表达式,例如12345或456789的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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