Remove literals from input mask after form submit?

邮差的信 提交于 2019-11-29 11:37:08

问题


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?


回答1:


I think you want to use unmask

$("#myForm").submit(function() {
  $("#txtPhone").unmask();
});



回答2:


Set removeMaskOnSubmit as true when you initialize the inputmask

    $("#txtPhone").inputmask({removeMaskOnSubmit: true});



回答3:


Based on Plugin Site:

$("#txtPhone").val($("#txtPhone").mask()); 



回答4:


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

    $('#amount').inputmask('999.999.999.999', {
        numericInput: true,
        autoUnmask: true,
    });



回答5:


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.




回答6:


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



回答7:


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.



来源:https://stackoverflow.com/questions/7854651/remove-literals-from-input-mask-after-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!