insin / inputmask-core

Standalone input mask implementation, independent of any GUI
MIT License
304 stars 52 forks source link

Get the value from the last character to left. #13

Open mauriciosoares opened 8 years ago

mauriciosoares commented 8 years ago

Hey, the title might seem a little confusing, but an example can make it very clear. Let's say I have the following pattern:

  var mask = new InputMask({pattern: '11/11/1111'});

  mask.paste('111');

If I use the getValue method, I get the string '11/1_/____'.

Is there a way to get only the characters from the last 1 to the left? it would return something like this: 11/1

thanks.

iamdustan commented 8 years ago

I believe there is a getRawValue() that will get you something close to that.

mauriciosoares commented 8 years ago

thanks @iamdustan

yes, getRawValue would return in the previous example 111.

But I do need the mask, but I need only the ones that are to the left of my last character, that's why in my example I get only one /. The number of characters retrieved does matter for my case...

the getValue would return a string with a length of 10. (even if I paste only a 111) the getRawValue would return a string with a length of 3. My example returns a string with a length of 4.

mauriciosoares commented 8 years ago

Just to let you know, I achieved this by doing some crazy hack, here's the snippet:

var instance = new InputMask({pattern: mask, value});
value.split('').forEach(s => instance.input(s));
const {end} = instance.selection;
return instance.getValue().slice(0, end);

Basically, I manually input each of the characters in the instance (had to use input instead of paste, if 1 single character is wrong in the paste method, it just dont paste anything);

After that I get the end property from the selection object, and slice the getValue value from 0 to the end number.

Still I'd like to know if there's a better approach :)