'jquery.inputmask unmasking empty email

I am using RobinHerbots/jquery.inputmask library.

Library returning _@_._ for empty value. You can try demo. Here is my code.

HTML:

<input type="text" id="email" autocomplete="off">
<button type="button" id="unmask">Unmask</button><br>
Value: '<span id="val"></span>'<br>
Unmasked value: '<span id="unmasked"></span>'

JS:

$("#email").inputmask('email');
$("#email").inputmask({'autoUnmask': true});

$("#unmask").click(function(){
    $("#val").text($("#email").val());
    $("#unmasked").text($("#email").inputmask('unmaskedvalue'));
});


Solution 1:[1]

Seeing as it stills get the given automask email pattern, One of the workarounds I would recommend is to find a given regex pattern and replace it to an empty string. This is to get and check if the given value is an empty string or note.

Given Example:

$("#email").inputmask('email');
$("#email").inputmask({
    'autoUnmask': true
});

$("#unmask").click(function() {
    var oEmailText = $("#email");
    $("#val").text(oEmailText.val());
    $("#unmasked").text(oEmailText.val());
    var sInitialValue = $.trim(oEmailText.val()).replace(/^[\s_@.]+$/g, '');

    $("#unmasked").text(sInitialValue);
    if ($.trim(sInitialValue) === '') {
        alert('Empty value');
        return false;
    }
});

Here's a JS Fiddle for further reference: http://jsfiddle.net/7o5sn8bf/

Hope this helps for your case.

PS : Though I would suggest that its okay not import the given mask library plugin if you are applying the [auto mask inputs] for [email inputs] only https://github.com/RobinHerbots/Inputmask

Solution 2:[2]

Finally I found solution without regexp and etc. Looks like library bugged and autoUnmask does not work. Redifine onUnMask give correct result.

JS code here:

$("#email").inputmask("email", { onUnMask: function(maskedValue, unmaskedValue) {
    return unmaskedValue;
}});

Here is fixed demo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 User 328 6227
Solution 2 Yuri Sitnikov