I have an input field, which I want to limit as follows:
- The input will be a comma-separated list
- Each string in the list must be limited to 1..3 characters
- The list can have as many items as the user wants
some examples:
1,4,6a,16b
1
abc,def,ghi,jkl,mno,pqr,stu,vwx,yzz
I have found the following jsfiddle: https://jsfiddle.net/2x8y1dhL/ which provides a starting point for creating an input mask, but it assumes a fixed number of items in the list, and a fixed length of each input.
What I want is the following logic:
- after the user inputs the third character in a row that isn't a comma, a comma is automatically inserted
I have worked on the jsfiddle you have found. Some notes:
The meat of the matter are the two functions
createMaskanddestroyMask:Update 1: Pasting input that is longer than six characters - i.e. needs to be split more than once - turned out to be problematic with the original answer. An explicit loop is needed as the
gmodifier does not help with that regex. Here's the updated fiddle:Update 2: However, we can get away without an explicit loop with this alternative regex that only uses a single capture - updated fiddle: