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<9555<91000<20

Its "alphabetic"

You need to parse them to int.

parseInt(max) and parseInt(min)

...

functionMinimumNValidate(){
       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 );
       } 
 }    

    functionMaximumNValidate(){
       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:

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

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