textbox allowing Decimals limit to 2 using javascript -


i have problem of limiting value after decimal in textbox 2.

i don't want use regex. can me how achieve this?

i not allowing characters , limiting decimal point 1,

here code:

function checknumeric(e) {       if (window.event) // ie      {         if ((e.keycode < 48 || e.keycode > 57) & e.keycode != 8 & !(e.keycode == 46 && e.target.value.indexof(".") == -1)) {             event.returnvalue = false;             return false;         }     }     else { // fire fox         if ((e.which < 48 || e.which > 57) & e.which != 8 & !(e.which == 46 && e.target.value.indexof(".") == -1)) {             e.preventdefault();             return false;         }     }     }   } 

and here html code:

 @html.textbox("costperseat", (object)item.costperseat, new{onkeypress="checknumeric(event); ", maxlength=10, style = "width:60px" }) 

please consider below:

textbox control in .aspx page below:

<asp:textbox id="textbox1" runat="server" width="180px" onkeypress="return onlydotsandnumbers(this,event)"></asp:textbox> 

see, onkeypress event, call 1 of javascript function below:

function onlydotsandnumbers(txt, event) {     var charcode = (event.which) ? event.which : event.keycode        if (charcode == 46) {         if (txt.value.indexof(".") < 0)             return true;         else             return false;     }      if (txt.value.indexof(".") > 0) {         var txtlen = txt.value.length;         var dotpos = txt.value.indexof(".");         if ((txtlen - dotpos) > 2)             return false;     }      if (charcode > 31 && (charcode < 48 || charcode > 57))         return false;      return true; } 

it helpful you.


Comments