Tuesday 15 April 2014

asp.net mvc - while MVC model value to Javascript get error NaN -



asp.net mvc - while MVC model value to Javascript get error NaN -

i have 2 textbox , 1 hidden input.

class="snippet-code-js lang-js prettyprint-override"> function isnumberkey(evt) { var charcode = (evt.which) ? evt.which : event.keycode if (charcode > 31 && (charcode != 46 && (charcode < 48 || charcode > 57))) homecoming false; homecoming true; } function getreceiveamount(ctr) { var x = $("#sendamount").val(); var y = $("#sendingexchangecommission").val(); alert(x) alert(y) var total = (x * y).tofixed(2); $("#recieveamount").val(total); } class="snippet-code-html lang-html prettyprint-override"> @model turkex.models.fromto @{ viewbag.title = "exchange"; layout = "~/views/layoutpages/sitelayout.cshtml"; } @using (html.beginform("exchange", "home", formmethod.post, new { enctype = "multipart/form-data" })) { <input type="hidden" value="@model.sendingexchangecommission" id="sendingexchangecommission" name="sendingexchangecommission"/> <div class="form-group"> <div class="exchange-amount-label"> <label>@turkex.globalization.exchanges.resource.amount</label> </div> <input class="exchange-amount" id="sendamount" name="sendamount" value="@model.gateway.mintransfer" onkeypress="return isnumberkey(event)" onchange="return getreceiveamount(this);" /> </div> <div class="form-group"> <div class="exchange-amount-label"> <label for="exampleinputemail1">@turkex.globalization.exchanges.resource.amount</label> </div> <input class="exchange-amount" id="recieveamount" name="recieveamount" @*value="@model.sendingexchangecommission"*@> </div> <button type="submit" value="exchange" class="exchange-button">@turkex.globalization.exchanges.resource.exchange</button> </div> <div class="clearfix"></div> }

but when come in number textbox 1 (id = sendamount) textbox 2 shows nan

if alter y manualy "10.1" there no problem.

var y = $("#sendingexchangecommission").val();

isnt work?

at first glance, there doesn't seem populating y variable in getreceiveamount():

// value empty/null if sendingexchangecommission empty: var y = $("#sendingexchangecommission").val();

so when seek calculate total here:

var total = (x * y).tofixed(2);

you might calculating numeric value x null (or nan) value, either give 0 or nan result:

// examples show how multiplying unknown // info types can give nan // number * string = nan var total = (1 * "somestring").tofixed(2); // --> nan // number * nan = nan var total = (1 * nan).tofixed(2); // --> nan // number * empty string = 0 var total = (1 * " ").tofixed(2); // --> 0.00 // number * 0 = 0 var total = (1 * 0).tofixed(2); // --> 0.00

so check create sure getting numeric value in y (#sendingexchangecommission) before calculating total. can see example, if y (#sendingexchangecommission) not empty, not numeric value, nan result.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //

update: want check if value within input field in fact, number. includes number values comma in them. if scheme of writing numbers in country uses comma instead of period separate values (0.0996 vs 0,0996), need scrub out comma, because look result in nan:

// comma in numeric string result in nan var total = (1 * "0,0996").tofixed(2); // --> nan

where look result in 996.0

// give proper number var total = (1 * "0.0996").tofixed(2);

so of import thing consider here, when grabbing values html text .val() , or .value, string returned you, if want perform numeric calculations on string, need sure string value has been converted number. if comma within string, value can't converted number.

lastly, maintain in mind parsefloat() give unusual behavior if feed comma within of numeric values, example, here output of parsefloat("0,0996") vs parsefloat("0.0996"), notice how parsefloat comma gives unusual behavior, but period works expected:

// results in 0 (zero) parsefloat("0,0996") // --> 0 // results in 0.0996 parsefloat("0.0996") // --> 0.0996

so think might simple comma in numeric values throwing off.

hope helps.

javascript asp.net-mvc model-view-controller nan

No comments:

Post a Comment