Skip to content Skip to sidebar Skip to footer

Compare Two Input Values In Input Validation Html Javascript?

I want to compare two inputs minimumN and maximumN, and display an alert if the logic is not satisfied, I have the following code: HTML:

Solution 1:

Few things here

close the input elements:

if(maxN<min) {

Should be

if(max<min) {

Finally, you are not comparing integers but strings so..

5<9
555<9
1000<20

Its "alphabetic"

You need to parse them to int.

parseInt(max) and parseInt(min)

...

function MinimumNValidate(){
       var min = parseInt(document.getElementById("minN").value);
       var max = parseInt(document.getElementById("maxN").value);
       if(min > max) {
           alert("Minimum value must be lesser than maximum value. " + min + " > " + max );
       } 
 }    

    function MaximumNValidate(){
       var min = parseInt(document.getElementById("minN").value);
       var max = parseInt(document.getElementById("maxN").value);
       if(max<min) {
           alert("Maximum value must be greater than minimum value."  + min + " > " + max );
       } 
  }

Solution 2:

In the second function MaximumNValidate(), you have the line of code

if(maxN<min) {

which should be

if(max<min) {

Solution 3:

function checknumber(theForm) {   
 if (parseInt(theForm.num2.value) != (parseInt(theForm.num1.value)+1)) 
    { 
    alert('enter the correct year in intermediate');
            return false;
            } 
     return true;
            
}
<!doctype html>
<head> </head> 
    <body> 
        <form action="../" onsubmit="return checknumber(this);">     
<label> tenth graduation </label> 
      <input type="number" name="num1"  autocomplete="off"><br> <br>
<label> Intermediate </label>
       <input type="number" name="num2" autocomplete="off">
     <input type="SUBMIT" value="year validation">
       </form> 

Post a Comment for "Compare Two Input Values In Input Validation Html Javascript?"