1

I have following regex to validate card number

[\d+]{6,6}[X|x]{8,8}[\d+]{4,4} 

I want to validate following as valid card number

 123456XXXXxxxx1234 

But anything extra then this should not match.

 123456XXXXxxxx1234a bbb123456XXXXxxxx1234nnn 

That is these should not match.

I tried this regular expression but it is not working for me.

^[\d+]{6,6}[X|x]{8,8}[\d+]{4,4}$ 
6
  • What isn't working with the second rege,? Commented Feb 6, 2016 at 21:28
  • Yes . With second regex . 123456XXXXxxxx1234 is coming as invalid Commented Feb 6, 2016 at 21:29
  • Are you sure you don't have trailing or leading whitespace? Commented Feb 6, 2016 at 21:30
  • Ohh yes. I have. Thanks for your help. Commented Feb 6, 2016 at 21:31
  • 1
    I think you misunderstand how to use character classes. Your [\d+]{4,4} matches 4 digits or plus symbols. I think you just meant \d{4} (4 digits). Right? Also: [X|x]{8,8} matches 8 X, x or | symbols. I guess you need just [Xx]{8}. Commented Feb 6, 2016 at 21:34

1 Answer 1

3

Your [\d+]{4,4} matches 4 digits or + symbols. I think you just meant \d{4} (4 digits). Also, [X|x]{8,8} matches 8 X, x or | symbols. I guess you need just [Xx]{8}.

I believe you need

^\d{6}[Xx]{8}\d{4}$ 

See regex demo

If you have time, you might want to read more about Character Classes or Character Sets.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.