Arun
Arun

Reputation: 685

How to validate a account number using javascript?

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.

Upvotes: 0

Views: 5000

Answers (2)

Haroon Yousuf
Haroon Yousuf

Reputation: 524

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*/

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882068

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: '\-')

Upvotes: 3

Related Questions