HI i have to compare three values but its not working..

alert box is not showing if i m putting less or greater values...i m calling this function on blur...

function validate(value) 
    {


        var minimum = document.getElementById('min').innerText;
        var maximum = document.getElementById('max').innerText;


      // alert(value);

      if( value < mimimum )
      {

         alert('Value is less than minimum value');

      }

      else if(value > maximum )
      {
          alert('Value is greater than maximum value');
      }
      else
      {

      }

    }

 </script>

Regards//

Dani AI

Generated

Two separate issues were stopping the comparisons: a misspelled variable and treating input content as text instead of numbers. was right that the values must be numeric, but also use the input element's value (not innerText) and fix the mimimum typo so you are not comparing against an undefined variable.

A simple, robust pattern is: read element.value, convert to a Number, check for NaN, then compare. Example:

function validate(inputEl) {
  var value = Number(inputEl.value);
  var min = Number(document.getElementById('min').value);
  var max = Number(document.getElementById('max').value);

  if (isNaN(value) || isNaN(min) || isNaN(max)) {
    // invalid numeric input; handle as needed
    return;
  }

  if (value < min) {
    alert('Value is less than minimum value');
  } else if (value > max) {
    alert('Value is greater than maximum value');
  }
}

For making the min[5] token dynamic in the class attribute (your validation plugin expects that format), either update the class string or use a data attribute. Example that replaces any existing min[...] token or appends one:

var input = document.getElementById('min');
var newMin = 8;

if (/min\[\d+\]/.test(input.className)) {
  input.className = input.className.replace(/min\[\d+\]/, 'min[' + newMin + ']');
} else {
  input.className += ' min[' + newMin + ']';
}

Better alternatives: use input.setAttribute('data-min', newMin) and change the validator to read that, or switch to type="number" and set input.min = newMin so the browser and many libraries can handle the constraint directly.

Troubleshooting tips: ensure the blur handler passes the element (e.g., onblur="validate(this)") or otherwise pass a numeric value; use console.log() to inspect types before comparing; if you use parseInt, always supply radix 10 (parseInt(str, 10)) or prefer Number()/parseFloat() for floats. , fix the typo and switch to .value and the alerts should work.

Recommended Answers

All 6 Replies

I think your want to make int comparisson, if sou use parseInt(document.getElementById('min').innerText). If it's float, use parseFloat().

Using > and < with string will not do you any good.

yes Sir working..

<input value="-7" class="validate[required,custom[integer],min[5]] text-input" type="text" name="min" id="min" />

here is one more problem..
i wanto use my javascript varible value in above class "validate[required,custom[integer],min[5]]" here like use dynamic number where 5 is using how to do it?

Sorry, I didn't understand what you want. Please rephrase it.

i m getting value from java script..
now i want to use that particular value in input element class
<input value="-7" class="validate[required,custom[integer],min[5]] text-input" type="text" name="min" id="min" />

u can see min[5] i want to use javascript value in min[javascript varible value]

how to do it?
regards

You can do it like this:

var input = document.getElementById('myInputID');
var className = input.className;

input.className = className.replace("int[5]", "int[7]");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.