igorescobar / jQuery-Mask-Plugin

A jQuery Plugin to make masks on form fields and HTML elements.
http://igorescobar.github.io/jQuery-Mask-Plugin/
Other
4.77k stars 1.42k forks source link

onChange callback event not firing #688

Open kouts opened 6 years ago

kouts commented 6 years ago

Device

Desktop

Browser (and version)?

Firefox, Chrome

Functional jsfiddle exemplifying your problem:

http://jsfiddle.net/q6cLkyo4/3/

Describe de problem depth:

onChange callback event not firing when selecting all the text inside the input and hitting the delete key. onchange_not_firing

adamasantares commented 5 years ago

This event doesn't fire when value of input is empty. Doesn't matter how you did it empty.

theladyjaye commented 5 years ago

I ran into this too... there's a few places where it's relevant in the source:

https://github.com/igorescobar/jQuery-Mask-Plugin/blob/master/src/jquery.mask.js#L389

This sets the first oldValue, if the initial value of your input is "" then the oldValue will be empty string. Now, the oldValue doesn't change again until the input is blur'd.

I vendor'd the plugin and made the following changes to get around it:

https://github.com/igorescobar/jQuery-Mask-Plugin/blob/master/src/jquery.mask.js#L123 Became:

oldValue = p.val() || null;

https://github.com/igorescobar/jQuery-Mask-Plugin/blob/master/src/jquery.mask.js#L389 Became:

var jMask = this, oldValue = p.val() || null, regexMask;

Note that both changes employ p.val() || null since '' is false-y

This could also be done in 1 alteration instead of 2 with the following added after line 384: https://github.com/igorescobar/jQuery-Mask-Plugin/blob/master/src/jquery.mask.js#L384

oldValue = val // # now line 385

This change on line 373/374 would also make it work:

var val = p.val(),
    tmpVal = val || null, // <---
    changed = tmpVal !== oldValue, // <---- using the tmpVal to check for a change

I'm using this one ^ in my vendor'd version. I keeps that data the same, and only uses that tmpVal which can be null, to check for changed.