I want to validate a string that must be of the format x.xx.xxxxx
, the first character is required, followed by a period, then two characters also followed by a period and then another set of characters can be anything of any length
-
You might want to check the position of '.' , in your case '.' mush be present at index 1 and 4 according to 0 based indexing.– MakarandApr 7, 2021 at 17:37
1 Answer
I tested out a RegExp pattern /[^.]\.[^.]{2}\.[^.]+/
in the Node console which might help you.
It requires a single non-period character, followed by a period, followed by two non-period characters, followed by a period, followed by one or more non-period characters.
If 0 or more non-period characters are allowed in the last position, then it should instead be as follows, ...
/[^.]\.[^.]{2}\.[^.]*/
> pattern = /[^.]\.[^.]{2}\.[^.]+/
/[^.]\.[^.]{2}\.[^.]+/
> pattern.test('a.bc.d')
true
> pattern.test('a.b.d')
false
> pattern.test('a.bc.djfkflkjd483789374')
true
In the context of yup it looks like this documentation would enable you to use a regexp for validation... https://github.com/jquense/yup#stringmatchesregex-regex-message-string--function-schema