html - Javascript keyup function throwing wrong alert message -


with following script, trying validate whether refund amount wlt_ln_refund_amt greater balance amount wlt_ln_bal using keyup function.

  • in html read field wlt_ln_bal (field type = number) have amount 222.00
  • the other field wlt_ln_refund_amt (field type = number)

the testcase

  • for value 3 system throwing error message "refund amount rs.3 greater balance rs.222.
  • for values 1, 2 or 2000 system not throwing errors

here html code:

<form id="lnrefund" name="lnrefund"      method="post" role="form"      class="form-horizontal"      action="<?php echo $_server['php_self']; ?>"      onsubmit="return (checkform() && confirm_update())">        <div class="form-group">      <label class="col-md-2 col-xs-12 control-label">loan balance</label>      <div class="col-md-3 col-xs-12">               <input id="wlt_ln_bal" name="wlt_ln_bal"          type="number"value ="<?php echo $bal ?>"          class="form-control required" readonly/>        <span class="help-block">required</span>      </div>    </div>    <label class="col-md-2 col-xs-12 control-label">refund amount</label>    <div class="col-md-3 col-xs-12">     <input id="wlt_ln_refund_amt"             name="wlt_ln_refund_amt"type="number" step="0.01"             class="form-control" required/>     <span class="help-block">required</span>    </div> </form> 

and javascript

<script type="text/javascript"> $(function(){     $("#wlt_ln_refund_amt").keyup(function () { var ref = document.lnrefund.wlt_ln_refund_amt.value; var bal = document.lnrefund.wlt_ln_bal.value; if (ref>bal)     {       alert('refund amount rs.'+ref+  '\nis greater available balance rs.'+bal)           return true;           }       });       }); </script> 

it looks variables being compared strings (i.e. alphabetically) should try like

        var ref = parseint(document.lnrefund.wlt_ln_refund_amt.value);         var bal = parseint(document.lnrefund.wlt_ln_bal.value); 

or maybe

        var ref = parsefloat(document.lnrefund.wlt_ln_refund_amt.value);         var bal = parsefloat(document.lnrefund.wlt_ln_bal.value); 

if you're expectiong decimals


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -