Why one and the same value in the input it valid, is it not?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
16 views
Hi all.

Faced with a strange thing when working with Angular forms.
Validation behaves in unpredictable ways, but was able to identify some patterns:
– Figures are always valid
The letters are valid when adding
– Letters invalid when deleting

Tell me, why it works and how to avoid such inconsistency?
How it looks in real life
5b967f62bfe83122120782.gif
In the console write the function:
validate(control FormControl) { const pattern: RegExp = namePatternExp; console.log(control.value pattern.test(toString(control.value))); return pattern.test(toString(control.value)) ? null : { validate: { valid: false } }; }

toString of Lodash – no effect. Without it the same.
namePatternExp like this: [a-zA-Z ]* (out of desperation took out of. Angular documentation)

by | 16 views

2 Answers

0 like 0 dislike
I suggest this simple experiment in the console:
5b968baf9dc20559535666.png
by
0 like 0 dislike
links for thought
\rjsfiddle.net/xuxicheta/41rvdwh0/7
\rhttps://angular-reactive-form-guuytj.stackblitz.io

\rhttps://learn.javascript.ru/regexp-methods#regexp-...
There is not written, but the regexp.test(str) c the flag g is the same as exec.
I.e. if there is g, regexp stops at the last successful match and remembers the index. The next call starts with him.

When you add 1 letter, the next test is fed to the segment starting from lastIndex, i.e. this is our last added to the letter. And it gives true. When you remove one letter, lastIndex is border-line and search with the error returns false.
In the case of figure regexp, finds nothing, and lastIndex stays 0, and because your condition is that any number of letters and a null number of letters also suitable, it returns true
by
110,608 questions
257,186 answers
0 comments
35,392 users