GoncharukOrg / react-input

100 stars 7 forks source link

replacement regex is only for single char #13

Closed naczu closed 11 months ago

naczu commented 11 months ago

I set the mask like below.

mask="0 (5__) _ __"

The replacement is working for single character. But I need to set a regex for whole mask. My regex is like this.

0 (5(0[1,5,6,7]|16|3[0-9]|4[1-9]|5[1,2,3,4,5,9]|61)) [0-9]{3} [0-9]{2} [0-9]{2}

GoncharukBro commented 11 months ago

Please read the documentation section at the link and reproduce the replacement property with the characters specified in your regex. If the mask has different lengths, then you need to replace the value of the mask property along with the replacement property if necessary.

https://www.npmjs.com/package/@react-input/mask#replacement

naczu commented 11 months ago

Well I read the documentation, but I think you don't get what I asked. Assume that you need to ask the user to enter a time by using the hh:mm format. The simpler implementation of the time input mask may use the __:__ mask, but users can still enter some invalid time entries, such as 59:10, 08:65, etc. So how to prevent entering these entries. when first char is 2, second char has to be 0-4. Where can we set conditions ? So if you look closer to regex which I gave above there are multiple conditions.

GoncharukBro commented 11 months ago

You can use the modify function (https://www.npmjs.com/package/@react-input/mask#modify). Regarding your time example, the solution would look like this:

<InputMask
  mask="Hh:Mm"
  replacement={{ H: /[0-2]/, h: /\d/, M: /[0-5]/, m: /\d/ }}
  modify={(input) => { 
    if (input[0] === '2') {
      return { replacement: { H: /[0-2]/, h: /[0-4]/, M: /[0-5]/, m: /\d/ } };
    }
  }}
/>

where the hour input becomes limited to "24".