0

I have defined the following validation:

            Validators.pattern("/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/")]]

Its to validate postal codes, but when I enter the string 'K1K1A1' the validator says its invalid.

What is wrong with my regex?

2
  • 2
    Try to remove the quotes. Commented Jun 11, 2017 at 18:13
  • postal codes isn't helpful. Which country postal codes? What is a valid postal code in your case? Commented Jun 11, 2017 at 18:34

1 Answer 1

7

Validators.pattern() looks like:

if (typeof pattern === 'string') {
  regexStr = `^${pattern}$`;
  regex = new RegExp(regexStr);
} else {
  regexStr = pattern.toString();
  regex = pattern;
}

So, just remove slashes and ^$ characters to fit with angular. Or you also remove quotes and javascript will take your expression as a RegExp type.

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

2 Comments

Yes, removing quotes the typeof is equals RegExp.
can you please help me to solve this stackoverflow.com/questions/52091334/… @Serginho

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.