Why your JavaScript regex passes only every second test
The same field passes validation, fails without changing, then passes again. Inspect the regular expression before blaming the input. A reused expression with the global g flag keeps a cursor called lastIndex. The sticky y flag is stateful too.
Expose the cursor behind the alternating result
An alternating error is particularly confusing because the user has done nothing wrong. A code may be accepted on the first submission, rejected on the second, then accepted again after another click. Before changing the input rules, find where the regular expression is created. A pattern stored outside a handler is reused; moving it inside the handler can hide the state problem without explaining it.
On a local test page or in a JavaScript runtime, keep one expression and call it repeatedly with one harmless value. Log the cursor before and after each call:
const check = /foo/g;
for (let attempt = 1; attempt <= 3; attempt++) {
const before = check.lastIndex;
const matched = check.test("foo");
console.log({ attempt, before, matched, after: check.lastIndex });
}
You should see the cursor move to 3 after a match, then return to 0 after failure. The next attempt starts at the beginning again. This makes the apparent randomness visible. Test on a disposable expression: diagnostic calls themselves advance a stateful regex and can alter the behavior you are trying to inspect.
A public 2013 report described this alternating behavior. The retained Node.js v24.19.0 fixture reproduced it with one expression and one string:
const pattern = /foo/g;
pattern.test("foo"); // true; lastIndex = 3
pattern.test("foo"); // false; lastIndex = 0
pattern.test("foo"); // true; lastIndex = 3
The first match moves the cursor to the end. The next call starts there, finds nothing, and resets it. A changed input string does not automatically reset the expression: the cursor belongs to the RegExp object.
Keep independent validation stateless
For a normal form check, change the validator once rather than sprinkling cursor resets throughout click handlers. Keep flags you actually need, such as case-insensitive matching, but remove global or sticky matching when each input must be evaluated independently. Search the codebase for other uses of that same object before changing it. A scanner that collects several matches may genuinely depend on its cursor.
The ABC-1234 example below is a format check, not a rule for every kind of identifier. If you require a single-line value, reject line breaks explicitly. JavaScript's end anchor has line-ending nuances, and multiline mode changes anchor behavior further. Choose whether surrounding whitespace is an error or an accepted normalization step, then tell users that rule rather than silently changing their data.
const code = /^[A-Z]{3}-\d{4}$/;
["ABC-1234", "ABC-1234", "ABC-1234"]
.map(value => code.test(value));
// [true, true, true]
Keep deliberate scanning separate
function independentTest(pattern, text) {
pattern.lastIndex = 0;
return pattern.test(text);
}
This helper mutates the supplied expression. Do not insert it into a scanning loop that deliberately advances through matches. Prefer a separate validator when scanning and validation coexist; array callbacks do not reset the cursor for you.
The retained tests produced [true, false, true] with the global expression, and three true results both without g and with an explicit reset. That is a focused Node result, not a full browser compatibility audit. If the failure remains after state is removed, inspect the expression, hidden characters and Unicode assumptions rather than adding more resets.
Verify the fix through the real form
Test the same valid value several times, then an invalid value, then the valid value again. Also test two different valid values consecutively. The answer should depend on each value, not on the order of submissions. Repeat that small sequence through the actual form so blur handlers, live validation and submit validation all use the intended rule.
If an error still appears, inspect the exact characters and the remaining flags. Do not add a reset after every call as a universal fix: that can break a deliberate match-scanning loop elsewhere. Keeping the validator separate from a scanner is easier to understand and maintain than making one stateful object serve both jobs.
