Check if there is a percentage symbol (%) next to the number

Asked

Viewed 224 times

0

How to check whether or not the % symbol exists along with the value?

The code below is a simplified version. The end result is that the variable taxas will or will not have the percentage next to the number.

If I have % I will do the calculation according to it. If I have % I will do the simple calculation.

Error: rates.includes is not a Function

$(document).ready(function() {

  var formaID = "1";
  var formaTaxa = "3.55";
  var formaTipo = "%";


  if (formaID == "") {
    var taxas = $("#formaTaxas").val("0.00");
  } else {
    var taxas = $("#formaTaxas").val(formaTaxa + formaTipo);
  }




  if (taxas.includes("%") == true) {
    console.log("Tem %");
  } else {
    console.log("Não tem %");
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

  • Duplicated and of the same author https://answall.com/questions/398486/usar-o-simblo-de-no-input-maskmoney?noredirect=1#comment782686_398486

2 answers

3

Exchange the taxas.includes for taxas.indexOf.

The function indexOf returns the character index in the string and if it does not exist, returns -1.

if (taxas.indexOf("%") > -1) {
    console.log("Tem %");
} else {
    console.log("Não tem %");
}

Example:

function hasPercent(value) {
  return value.indexOf('%') > -1 ? true : false;
}

console.log('30 -> ' + hasPercent('30'));
console.log('30% -> ' + hasPercent('30%: '));

  • Vitor, it’s wrong to do it this way var formaTaxas = $("#formaTaxas").val(formaTaxa+formaTipo); that is, I am storing the value within the variable and at the same time playing the value formaTaxa+formaTipo to the imput #formaTaxas.?

  • It is not wrong depending on the end you want. Why you are setting the value for the input? In the example you passed you will never have the symbol % because you define the value for 0.00.

2


Turns out you’re using the .includes() the object of the element and not its value, so the error.

In doing:

var taxas = $("#formaTaxas").val("0.00");

You are changing the value of the element #formaTaxas and assigning to the variable taxas the element itself.

The method .includes() only works with arrays or with strings.

You’d have to use .val() before the .includes() to take the value of the field and check if it contains the character:

taxas.val().includes("%")

Example:

$(document).ready(function() {

  var formaID = "1";
  var formaTaxa = "3.55";
  var formaTipo = "%";


  if (formaID == "") {
    var taxas = $("#formaTaxas").val("0.00");
  } else {
    var taxas = $("#formaTaxas").val(formaTaxa + formaTipo);
  }




  if (taxas.val().includes("%") == true) {
    console.log("Tem %");
  } else {
    console.log("Não tem %");
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="formaTaxas">

Just keep in mind the fact that .includes() is less compatible than .indexOf(), which can, in that case, do the same thing.

See tables in Can I Use: .includes() / .index()

Browser other questions tagged

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