How to Handle numbers , dot , plus , minus Using JavaScript...?


How to Handle numbers, Dot , Plus ,minus Using JavaScript...? In this Artical i'm trying to explain how to handle numbers, dots , plus , minus while enter values in UI textbox, we prevent and rearrnage numbers using Javascript..

we just write a script for that and call that function in textbox onkeypress event...

That event call the javascript and display in order while enter the values in textbox..

1) While enter value in your textbox we try to enter value after signs then it's corrected that sentense and arrange in proper way...

ex: 12.34+ then it's corrected that value and re arrange +12.34

2) If we try to enter sign in between numbers then also it's re arrange in proper way..

ex: 12.+34 then it's corrected that value and re arrange +12.34

1) to handle number format
2) to handle Signs + , -.

JavaScript Code :




<script type="text/javascript">
function IsNumFieldKeyPress() {
var element;
element = event.srcElement;
if (event.keyCode >= 48 && event.keyCode <= 57) {//to handle num 0 -9
event.returnValue = true;
return;
}
else if (event.keyCode == 46) { // to handle "."
if (element.value.indexOf('.', 0) == -1) {
event.returnValue = true;
return;
}
}
else if (event.keyCode == 43) { // to handle +
if (element.value == undefined)
element.value = "+";
else if (element.value.charAt(0) != "+" && element.value.charAt(0) != "-")
element.value = "+" + element.value;
}
else if (event.keyCode == 45) { // to handle -
if (element.value == undefined)
element.value = "-";
else if (element.value.charAt(0) != "-" && element.value.charAt(0) != "+")
element.value = "-" + element.value;
}
event.returnValue = false;
}
</script>



Source Code :




<asp:TextBox ID="txtMaterialQty" runat="server" width="90%" onkeypress="IsNumFieldKeyPress();" Enabled="false" Tooltip="Enter Additonal Quantity (+) or (-), Not Total Quantity" />


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: