Capture value of a text without the mascara?

0

I have a text masked field as follows:

$("#txtnuminic").mask("99999999-9");

And I'm trying to extract the contents of it without the mask like this:

valor = $("input[type='text']").get(indice).value;

I already know that I need to use replace, but how would I look?

    
asked by anonymous 28.09.2017 / 16:12

3 answers

0

In documentation says that you have to use cleanVal() to get the value without the mask.

$("#txtnuminic").mask("99999999-9");

$('button').click(function (){
  console.log($('#txtnuminic').cleanVal());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>

<input type='text' id='txtnuminic'/>

<button>Pegar Valor</button>
    
28.09.2017 / 16:20
0

Not necessarily with replace, but if so it is as follows

$("#txtnuminic").mask("99999999-9");

$('button').click(function (){
  str = $("#txtnuminic").val();
  str = str.replace(/[^\d]+/g,"");
  console.log (str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>

<input type='text' id='txtnuminic'/>

<button>Pegar Valor</button>
  

regex removes anything other than a digit

    
28.09.2017 / 16:29
0

You need to use the cleanVal() method, see documentation .

Applying to your example, using get(indice) would look like this:

Note that the return of get(indice) is surrounded by $() , because the cleanVal function is an extension of jQuery elements and the return of get() is not a jQuery element.

$("#txtnuminic").mask("99999999-9");

$('button').click(function (){
  let val = $($("input[type='text']").get(0)).cleanVal();
  console.log(val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>

<input type='text' id='txtnuminic'/>

<button>Pegar Valor</button>
    
28.09.2017 / 18:06