简体   繁体   中英

If statement executing both if and else

I'm building a web app that converts Galloons to Liters and vise versa. Got one textbox to enter gallon/litters, the user selects on a radio button what they want to convert too. Now the problem arises when validating the input: for liters it must be Greater than 0 but less than 1000 for the gallons it must be greater than 0 but less than 4000 . So if I've selected liters it must validate only liters but both validations are coming up. Here's my code:

Form :

<body onload="setup()">
  <div data-role="page">
    <div style="padding: 20px;">
      <div data-role="fieldcontain">
        <input type="number" id="temperature" name="temperature">
        <label id="label">Gallons</label>
      </div>
      <fieldset data-role="controlgroup">
        <legend>Convert to:</legend>
        <input type="radio" name="units" id="Gallons" value="Gallons"
        onclick="setUnits('Liters')">
        <label for="Gallons">Gallons</label>
        <input type="radio" name="units" id="Liters" value="Liters"
        checked="checked" onclick="setUnits('Gallons')">
        <label for="Liters">Liters</label>
      </fieldset>
      <input type="button" onclick="convert()" value="Convert">
      <p id="answer"></p>
    </div>
  </div>
</body>

JavaScript :

function setup() 
{
    var cType;
    setUnits("Gallons");
    cType = "Gallons"; 
  
  document.getElementById("Gallons").onclick =
    function () {
        cType="";
        cType="Liters";
      setUnits("Liters");  
      
    CheckInput(cType);
    };
    

 document.getElementById("Liters").onclick =
    function () {
        cType="";
        cType="Gallons";
      setUnits("Gallons");
        
    CheckInput(cType);
    };
    
    
CheckInput(cType);
}

function setUnits(unit) {
  var label = document.getElementById("label");
  label.innerHTML = unit;
}



function CheckInput(cType) {
    var CheckInputcType= cType;
    
 var angleInput = document.getElementById("temperature");
    
    if(CheckInputcType.localeCompare("Gallons")==0)
    {       
       angleInput.addEventListener("blur",validateG);
    
    }
    else(CheckInputcType.localeCompare("Liters")==0)
    {
        angleInput.addEventListener("blur",validateL);
    }
  
}


function validateL(){
    
    var angleInput = document.getElementById("temperature");
    
  if (angleInput.value >= 1000 || angleInput.value<=0) 
  {
    alert('Liters must be between 0 and 1000');
    angleInput.value = "";
  }
}

function validateG() {
  var angleInput = document.getElementById("temperature");
   
  if (angleInput.value >= 4000 || angleInput.value<=0) 
{
    alert('Gallons must be between 0 and 4000');
    angleInput.value = "";
 }
}

You have a problem with your method CheckInput(cType), because every time you click on Galons or liters radio button you are adding a new listener event on temperature input. Just simply create one listener and verify radio state on that validator method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM