1

I have a <form> where I'm trying to compare the value and the max of an input so that if the value exceeds the max I'd apply different styling to the input (like a red border). Suppose I have an input like this:

<input id="inp_1" type="text" numeric-only="" max="50">

And let's assume I have three input values (9, 49 and 59). I'll come back to this is in a moment.

This is my js code that acquires the input and checks on the value and max:

var activeInpId = document.activeElement.id;
var activeInpVal = document.activeElement.value;
var activeInpMax = document.activeElement.max;
var pickElement = document.getElementById(activeInpId);
//console 1
console.log("The entered value is (" + activeInpVal + ") and it's max is (" + activeInpMax + ")");

if( activeInpVal > activeInpMax ){
   //console 2
   console.log("Error. Value (" + activeInpVal + ") exceeds max (" + activeInpMax + ")");

   pickElement.style.border = '2px solid';
   pickElement.style.outline = 'none';
   pickElement.style.borderColor = '#E60000';
   pickElement.style.boxShadow = '0 0 10px #E60000';
}else{
   console.log("Current value in range");

   pickElement.style.border = '';
   pickElement.style.outline = '';
   pickElement.style.borderColor = '';
   pickElement.style.boxShadow = '';
}

Problem

Now when I enter my input values 49 or 59 everything works fine but 9 acts as an error. Here's a screenshot of all 3 scenarios. enter image description here

Moreover the console messages in the code above outputs

The current value in focus is (9) and it's max is (50) for console 1, and

Error. Value (9) exceeds max (50) for console 2.

What could I be missing or doing wrong?

Clint_A
  • 518
  • 2
  • 11
  • 35

3 Answers3

3

So here the concept is if we do

console.log( '9' > '50'); //logs true
console.log( 9 > 50);    //logs false

so all you need to do to type cast values to integer before comparison.

Anki
  • 407
  • 3
  • 8
2

just put parseInt in your if condition like

if( parseInt(activeInpVal) > parseInt(activeInpMax) )

it works properly.

1

You need to parse the the strings to integers to be able to compare them using >

var inputs = document.querySelector(".input");
inputs.addEventListener("input", validate, false);

function validate(){
  addEventListener("input", function() {
    var activeInpId = document.activeElement.id;
    var activeInpVal = document.activeElement.value;
    var activeInpMax = document.activeElement.max;
    var pickElement = document.getElementById(activeInpId);
    //console 1
    console.log("The entered value is (" + activeInpVal + ") and it's max is (" + activeInpMax + ")");
    console.log(typeof activeInpVal);
    console.log(typeof activeInpMax);
    if (parseInt(activeInpVal) > parseInt(activeInpMax)) {
      //console 2
      console.log("Error. Value (" + activeInpVal + ") exceeds max (" + activeInpMax + ")");

      pickElement.style.border = '2px solid';
      pickElement.style.outline = 'none';
      pickElement.style.borderColor = '#E60000';
      pickElement.style.boxShadow = '0 0 10px #E60000';
    } else {
      console.log("Current value in range");

      pickElement.style.border = '';
      pickElement.style.outline = '';
      pickElement.style.borderColor = '';
      pickElement.style.boxShadow = '';
    }
  });
}
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
<input class="input" id="inp_1" type="text" numeric-only="" max="50">
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73