1

I have to create a regex to validate ticket string in correct format.

Ticket format:

SFHD00002523003

It starts with four alphabets and ends with 12 numeric characters.

This is my code in my angular controller:

var pattern = new RegExp('[A-Z]{4}\\d{12}$');
console.log(pattern.test('SFHD00002523003');

Unfortunately, it is returning false for correct string, too.

5
  • Try var pattern = /^[A-Z]{4}\d{12}$/; Commented Feb 2, 2016 at 13:20
  • Unless I've forgotten how to count, those are not 12 digits in your test string... ಠ_ಠ Commented Feb 2, 2016 at 13:28
  • Thanks wiktor it is working Commented Feb 2, 2016 at 13:30
  • It is working thank you Commented Feb 2, 2016 at 13:51
  • You need to use @ and then the user name without spaces to make sure the user is notified. I came back here by chance. Posted as an answer. Commented Feb 2, 2016 at 21:50

2 Answers 2

1

Need double backslash instead of single. like: \\d instead of \d in your expression

var pattern= new RegExp('([A-Z]){4}\\d{12}');

and your string has 11 numeric chars not 12

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

Comments

1

You forgot the start of string anchor ^ to make sure you only match 16 character string. If you do not use it, you will get a match inside a 17+ character string that ends with your pattern.

Thus, use

var pattern = /^[A-Z]{4}\d{12}$/;
               ^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.