Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 272
Terry Nederveld Avatar asked Apr 05 '10 19:04

Terry Nederveld


5 Answers

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

$("#OfficePhone").unmask();
like image 63
Diego Mendes Avatar answered Nov 08 '22 12:11

Diego Mendes


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>
like image 21
Terry Nederveld Avatar answered Nov 08 '22 11:11

Terry Nederveld


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

    var maskedInput = $("#id").data('mask');
    if (maskedInput) {
        maskedInput.remove();
    }
like image 3
Agni Avatar answered Nov 08 '22 13:11

Agni


Remove the inputmask:

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

or

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

or

Inputmask.remove(document.getElementById(selector));
like image 3
EngineerCoder Avatar answered Nov 08 '22 12:11

EngineerCoder


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();
like image 3
zed Avatar answered Nov 08 '22 12:11

zed