开发者

Validating field to contain only numbers separated by commas

I'm making a form validation callback in code igniter. I'm trying to validate an input that needs to be numbers separated by commas, no spaces. For example

开发者_如何学JAVA

1,2,3,4,5 221,78,4,82,991,12 10001,10010,20010 etc

Whats the best way to validate this regex? Some other PHP wizardry?


Why not a regex like this?

/^(\d+,)*\d+$/


Here's an expression that won't require any backtracking.

/^(?:\d+(?:,|$))+$/

The non-capturing groups (?:regex) also make it faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜