正则表达式为序列号,如12345或456789 javascript [英] regular expression for sequential numbers like 12345 or 456789 javascript

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

问题描述

我需要阻止在文本框中输入5个等级序列号,如12345,45678等。

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

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

I have 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 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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