1

I tried to implement a pattern validator with a built in validator in angular. I wanted to use this patter: /\S+/g but it didn't work because angular overwrites my pattern in its function You can see it here Why do they put the ^ and a $ at the beginning and the end of the string? Should I use a custom validator instead the built in? Is it a bug or working as expected?

Here is a plunkrhttp://plnkr.co/edit/OHrUitqUnhIYxsraby7J

4
  • The anchors are only added to a string pattern. If you pass a regex object (as a string), no anchors will be added. Also, you cannot use /g in the ng-pattern regex. Could you share a fiddle with your validator? Commented May 31, 2016 at 12:36
  • I have updated. Here it is Commented May 31, 2016 at 13:04
  • 1
    Ok, so, it means that the validator does not allow a regex object like string. Since you want to require at least one non-whitespace char, why not use ".*\\S.*"? Commented May 31, 2016 at 13:11
  • Thanks a lot. Put it in an anser and I can accept it. Commented May 31, 2016 at 13:16

1 Answer 1

2

It seems that this validator does not allow a regex literal to be passed. You need to make sure you match the whole string. So, if you plan to require the string to have at least one non-whitespace character, use

".*\\S.*"

See the updated plunkr.

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.