How to get the jQuery MaskedInput unmask() function to work properly?
Asked Answered
U

8

18

I am trying to have one input on my page that I would like to have a U.S. phone number mask by default. If a end user clicks a checkbox specifing they would like to enter a International phone number I want the mask to be removed.

I have tried multiple ways and have been unsuccessful thus far. In the current project I am using jQuery to hide/show a completely different input. But I don't like that option and would like a more streamlined approach.

I am using the following:

jQuery 1.4.1 (going to upgrade to 1.4.2 soon) and jQuery.MaskedInput-1.2.2

<script type="text/javascript">
$(document).ready(function($) {
  if ($("#InternationalOfficePhone").attr('checked') == false) {
    $("#OfficePhone").mask("(999) 999-9999? x99999");
  }
});

$("#InternationalOfficePhone").click(function() {
  if ($("#InternationalOfficePhone").attr('checked') == true) {
    //$("#OfficePhone").mask(); //doesn't work
    //$("#OfficePhone").unmask(); //doesn't work
    $("#OfficePhone").unmask("(999) 999-9999? x99999"); //doesn't work
  } else {
    $("#OfficePhone").mask("(999) 999-9999? x99999");
  }
});
</script>

The code above works properly to set the default mask, but no matter what I try on the click event for the InternationalOfficePhone it doesn't remove the mask.

Any help is much appreciated.

Ulrich answered 5/4, 2010 at 19:17 Comment(1)
The error I am getting is on the un/mask lines and it returns an "Object doesn't support this property or method"Ulrich
U
19

I was able to figure out the code to get it to work. Below is the code I am using now.

<script type="text/javascript">
$(function($) {
  $("#OfficePhone").mask("(999) 999-9999? x99999");
  $("#InternationalOfficePhone").click(function() {
    var mask = "(999) 999-9999? x99999";
    if ($("#InternationalOfficePhone").is(':checked')) {
      $("#OfficePhone").unmask(mask);
    } else {
      $("#OfficePhone").mask(mask);
    }
  });
});
</script>
Ulrich answered 5/4, 2010 at 20:13 Comment(2)
.unmask() works perfectly. All I needed, thanks. But as pointed out in Diego's answer, you do not need to pass a parameter to the function.Tingly
Does this still work with maskedInput 1.4.1? I'm finding that neither .unmask() nor .unmask(maskString) removes the functionality from the field nor the mask-inserted characters from the value. I had to call .mask() again (no arguments) to get the unmasked value, as suggested by this answer belowJamilajamill
A
30

You don't need to pass a parameter to the unmask function! Use it without a parameter and it works well.

$("#OfficePhone").unmask();
Aves answered 3/12, 2010 at 19:11 Comment(5)
Confirmed! Works without parameter. This should probably have been a comment though...Tingly
Why a comment if this is the answer to the question?Aves
There was already an answered that used the unmask function, and your answer reads as if it is building upon that answerTingly
off course not, the accepted answer is wrong. Terry use .unmask(parameter) and the right answer is .unmask(). This is why I put as a new answer, that time I couldn't edit others.Aves
Accepted answer is not wrong, but the parameter is not necessary. It still works with the parameter. But I did upvote your answer because it is more correct, given this.Tingly
U
19

I was able to figure out the code to get it to work. Below is the code I am using now.

<script type="text/javascript">
$(function($) {
  $("#OfficePhone").mask("(999) 999-9999? x99999");
  $("#InternationalOfficePhone").click(function() {
    var mask = "(999) 999-9999? x99999";
    if ($("#InternationalOfficePhone").is(':checked')) {
      $("#OfficePhone").unmask(mask);
    } else {
      $("#OfficePhone").mask(mask);
    }
  });
});
</script>
Ulrich answered 5/4, 2010 at 20:13 Comment(2)
.unmask() works perfectly. All I needed, thanks. But as pointed out in Diego's answer, you do not need to pass a parameter to the function.Tingly
Does this still work with maskedInput 1.4.1? I'm finding that neither .unmask() nor .unmask(maskString) removes the functionality from the field nor the mask-inserted characters from the value. I had to call .mask() again (no arguments) to get the unmasked value, as suggested by this answer belowJamilajamill
M
3

I run through the same problem, and what helped me is:

    var maskedInput = $("#id").data('mask');
    if (maskedInput) {
        maskedInput.remove();
    }
Manvell answered 6/1, 2015 at 7:15 Comment(0)
C
3

Remove the inputmask:

$(selector).inputmask('remove');

or

var input = document.getElementById(selector);
if (input.inputmask)
  input.inputmask.remove()

or

Inputmask.remove(document.getElementById(selector));
Clientage answered 8/3, 2017 at 8:34 Comment(5)
After $(selector).mask(...) $(selector).inputmask is undefined.Adriaadriaens
Uncaught ReferenceError: Inputmask is not definedAdriaadriaens
@Crimean.us inputmask library must be loaded. github.com/RobinHerbots/InputmaskClientage
Thank you @ClientageAdriaadriaens
Question is related to jquery.maskedinput (github.com/digitalBush/jquery.maskedinput) plugin, not to Inputmask (github.com/RobinHerbots/Inputmask).Goldfilled
S
3

I'm using jQuery Mask Plugin v1.14.12

In my case, the .unmask() wasn't working because I was using a different selector than the one used when the mask was first created.

For example, let's say this was the input:

<input type="text" class="someclass maskme" value="" name="somename" />

the mask was created like this:

$('.maskme').mask('0000');

then, I was trying to unmask it as follows, which wasn't working:

$('.someclass').unmask();

once I changed it to the following, it worked as expected:

$('.maskme').unmask();
Spracklen answered 1/7, 2020 at 21:3 Comment(0)
O
2

Try using the :checked instead of attr check, I think that is the problem. I mean use $('#InternationalOfficePhone').is(':checked') in the if condition.

Onassis answered 5/4, 2010 at 19:23 Comment(3)
That didn't make a difference. I am still getting an error on the $("#OfficePhone").unmask() code.Ulrich
What is the error you are getting? Check isMasked before doing the unmaskOnassis
See my posted answer. I was able to finally figure it out.Ulrich
M
1

I am using query.maskedinput.js Version: 1.4.1

the unmasking is done by

$("input").blur(function() {
    $("#info").html("Unmasked value: " + $(this).mask());
});

example Masking is

$("#pannumber").mask("aaaaa 9999 a");

Unmasking

$("#pannumber").blur(function() { 
    var pannumber=  $(this).mask() ;
});
Mahla answered 13/7, 2017 at 8:37 Comment(0)
G
0

I am using inputmask js Version: 5.0.2.

& am using below techniques for masking and unmasking

$("#mailPhone").mask('(999) 999-9999'); //masking.

Or

$('#mailPhone').inputmask('(999) 999-9999') //masking.

&

$('#mailPhone').inputmask("remove");  //unmasking.
Gruel answered 28/7, 2021 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.