/^[a-d][a-d]*(?:_[a-d]+)*$/
I'm using the above regex in jquery where I call it on every keypress on an input field. It works exactly as intended which is a problem because I'm running a dynamic update too. You can tell for yourself what it does, but the point here is the underscore _
. It's meant to be followed by a character a-d
and therefore cannot be the last thing the user types. This is my problem.
Since the regex is being checked against something that's being typed (in progress), it's not being checked against the final version. With every keypress, I check the validity of the regex, and if no match, I remove the offending character (instantly, the user can't get to see it, it doesn't register in the input field). This means that if the user types some_
, the dynamic correction kicks in and takes it back to some
(even if the user is in the middle of typing a continuation.
Is there any way I can slow down the keypress or any other solution in this case?