Capture the value of a text without the mask?

Asked

Viewed 1,204 times

0

I have a text field masked as follows:

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

And I’m trying to extract the contents of it without the mask as follows:

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

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

4 answers

0

In the documentation says 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>
<script src="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>

  • Okay, but in that case it doesn’t work if you go through the index, like this ?

  • @Ninja2112 Why not use the documentation method? It’s not easier?

0

Not necessarily with replace, but if you wish 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>
<script src="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 that is not typed

0

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

Applying to your example, which uses the get(indice) would be so:

Note that the return of get(indice) is surrounded between $(), because the function cleanVal 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>
<script src="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>

0

In a row:

valor = $("input[type='text']").get(indice).value.toString().replace(/[^\d]+/g, '');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.