Compare 2 Values in Javascript -


i have problem compare values in javascript, here code :

     if(removecomma(f.txt_mintransfee.value) >= removecomma(f.txt_maxtransfee.value)){          alert("minimum transfer fee must less maximum transfer fee");          return;      }      if(removecomma(f.txt_minexfee.value) >= removecomma(f.txt_maxexfee.value)){          alert("minimum express fee must less maximum express fee");          return;      }      if(removecomma(f.txt_minearsfee.value) >= removecomma(f.txt_maxearsfee.value)){          alert("minimum settlement fee must less maximum settlement fee");          return;      } 

i tried values :

txt_mintransfee = 1 , txt_maxtransfee = 22 txt_minexfee = 2 , txt_maxexfee = 22 txt_minearsfee = 3 , txt_maxearsfee = 22 

but problem cannot pass 3rd function, alert "minimum settlement fee must less maximum settlement fee"

first, parse strings numbers, handling errors invalid input. then, compare numbers.

 var mintransfee = +removecomma(f.txt_mintransfee.value);  var maxtransfee = +removecomma(f.txt_maxtransfee.value);  var minexfee = +removecomma(f.txt_minexfee.value);  var maxexfee = +removecomma(f.txt_maxexfee.value);  var minearsfee = +removecomma(f.txt_minearsfee.value);  var maxearsfee = +removecomma(f.txt_maxearsfee.value);   if (isnan(mintransfee) ||      isnan(maxtransfee) ||       isnan(minexfee) ||       isnan(maxexfee) ||       isnan(minearsfee) ||       isnan(maxearsfee)) {      // handle error, invalid number input      return;  }   if (mintransfee >= maxtransfee) {      alert("minimum transfer fee must less maximum transfer fee");      return;  }  if (minexfee >= maxexfee) {      alert("minimum express fee must less maximum express fee");      return;  }  if (minearsfee >= maxearsfee) {      alert("minimum settlement fee must less maximum settlement fee");      return;  } 

note: above treat "" 0. if don't want that, need check strings "" before converting number via +.

alternately: whenever have repetitive code that, think in terms of factoring common parts function:

function checkminmax(f, type, msg) {      var minfeestr = removecomma(f["txt_min" + type + "fee]".value.trim());      var maxfeestr = removecomma(f["txt_max" + type + "fee]".value.trim());      var minfee = +minfeestr;      var maxfee = +maxfeestr;       if (!minfeestr || !maxfeestr || isnan(minfee) || isnan(maxfee)) {          // 1 of them blank or invalid          // show message          return false;      }       if (minfee >= maxfee) {          alert(msg);          return false;      }       return true; } 

...which can use this:

 if (!checkminmax(f, "trans", "minimum transfer fee must less maximum transfer fee") ||      !checkminmax(f, "ex", "minimum express fee must less maximum express fee") ||      !checkminmax(f, "ears", "minimum settlement fee must less maximum settlement fee")) {      return;  } 

note that version checks "" , considers them invalid.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -