最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to validate a account number using javascript? - Stack Overflow

programmeradmin2浏览0评论

var expression=/[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;

This is the expression iam using for validating a account number. it is working very well. but i need to validate it by - instead of space. how can i do it?

eg: XXXX-XXXX-XX-XXXXXXXXXX (4+4+2+10)

Thanks.

var expression=/[0-9]{4}\s[0-9]{4}\s[0-9]{2}\s[0-9]{10}/;

This is the expression iam using for validating a account number. it is working very well. but i need to validate it by - instead of space. how can i do it?

eg: XXXX-XXXX-XX-XXXXXXXXXX (4+4+2+10)

Thanks.

Share Improve this question asked Nov 15, 2012 at 6:41 ArunArun 6855 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Just replace all those '\s' markers with '-'. Outside of the character class range '[]', '-' is treated as a normal character (inside the range, you would have to escape it thus: '\-')

Replace \s with - you get:

var expression=/[0-9]{4}-[0-9]{4}-[0-9]{2}-[0-9]{10}/;
expression.test('4444-4444-22-01234567890') /*return true*/

Replace \s with "" you get:

var expression=/[0-9]{4}[0-9]{4}[0-9]{2}[0-9]{10}/;
expression.test('444444442201234567890') /*return true*/
发布评论

评论列表(0)

  1. 暂无评论