22

this question has already been asked but the solutions where not clear.

Im using Josh Bush's MaskedInput plugin for jQuery

What im trying to achieve is:

E.g: a phone input with the mask

    $("#txtPhone").mask("(99)9999-9999");

EQUALS: (00)9398-8373

i want it to submit : 0093988373

------ Is it possible to remove the .mask on submit but keep the value?

JW8
  • 1,496
  • 5
  • 21
  • 36
Wednesday Man
  • 359
  • 2
  • 3
  • 12

7 Answers7

35

I think you want to use unmask

$("#myForm").submit(function() {
  $("#txtPhone").unmask();
});
Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
16

Set removeMaskOnSubmit as true when you initialize the inputmask

    $("#txtPhone").inputmask({removeMaskOnSubmit: true});
  • 1
    This code you submit is relative to `jquery.inputmask` from "Robin Herbots" ( https://github.com/RobinHerbots/jquery.inputmask ). This does not work on Josh Bush's `jquery.maskedinput` ( https://github.com/digitalBush/jquery.maskedinput ). – lucasarruda Jan 27 '16 at 20:51
  • Yes your are right, do you want me to delete the answer? – Shameem Ahmed Mulla Feb 08 '16 at 09:37
8

Based on Plugin Site:

$("#txtPhone").val($("#txtPhone").mask()); 
dperez
  • 592
  • 5
  • 11
7

unmask is not a function. This is my way of use. autoUnmask: true

    $('#amount').inputmask('999.999.999.999', {
        numericInput: true,
        autoUnmask: true,
    });
umutyerebakmaz
  • 887
  • 8
  • 18
2

If you are using the completed callback, then you can just use:

$(element).mask("(99)9999-9999", {
  completed : function () {
    var numbers = this.val().replace(/()-/g,'');
  }
}

Otherwise, you can use:

$(element).val().replace(/()-/g,'');

If you want to do this before submitting, I suggesting capturing the submit event, using the code immediately above and then submitting the form.

EDIT: Alex Peattie pointed out the unmask() function, which I must say is a much better solution than mine. I'll leave my answer here, but I'd go with his solution.

Ivan
  • 10,052
  • 12
  • 47
  • 78
  • TY vm @Ivan// i will keep your solution in mind for other coding that dont using the .mask plugin – Wednesday Man Oct 24 '11 at 12:30
  • Using a regexp didn't work for me. I am using the same mask plugin with the following pattern: $?999. .replace(/$_/g,'') did not work. I tried escaping the $ with a backslash and that didn't work. For now I am using .replace('$','').replace(/_/g,'') and that works. – wclark Oct 21 '12 at 17:40
0

You can also extract the raw value on a masked input using

$("#YourSelector").data( $.mask.dataName )();

Source : https://github.com/digitalBush/jquery.maskedinput/issues/318


Example
If you use a phone input with such a mask :

$(".phone").mask("99 99 99 99 99")

you can extract the user input using :

$(".phone").data($.mask.dataName)() 
// will produce "0102030405" while the masks displays 01 02 03 04 05
AlexB
  • 7,302
  • 12
  • 56
  • 74
0

Assuming you have more than just one input being submitted, you could use:

// unmask all inputs, before savinf;
$(FormObj+" input[type=text]").each(function() {
    $(this).unmask();
});

where FormObj is your form object id, such as #form.

Paulo Henrique
  • 489
  • 3
  • 15