Depending on user requirements, format the amount you enter, that is, each three-digit number is separated by commas and retains two decimal places.
Taking into account the user experience, with the use of JS to format the amount, the foreground code is as follows:
Copy Code code as follows:
<asp:textbox id= "Txtamount" runat= "Server" onkeypress= "Check ()" onkeyup= "Run (This)" ></asp:TextBox>
The JS code is as follows:
Copy Code code as follows:
= = = = = Check whether the input is a number
function Check () {
if (!) ( (Window.event.keyCode >= && window.event.keyCode <= 57) | | Window.event.keyCode = 46 | | Window.event.keyCode = = 45)) {
Window.event.keyCode = 0
}
}
= = = = Amount of formatted text box
function run (obj) {
var objvalue = Obj.value.replace (/[,]/g, ""),
Objlength = Objvalue.length,
dTMP = Objvalue.indexof ("."),
Neg = Objvalue.indexof ("-");
var inttmp = 0,
Floattmp =-1;
if (dtmp!=-1) {
inttmp = dTMP = = 0? "0": New String (ObjValue). substring (0, dtmp);
floattmp = new String (objvalue). substring (dtmp + 1, objlength + 1);
Floattmp = Floattmp.replace (/[^0-9]/g, "");
}
else {
Inttmp = ObjValue;
}
if (neg = = 0) {
Inttmp = Inttmp.replace (/[-]/g, "");
}
Inttmp = Inttmp.replace (/[^0-9]/g, "");
var tmp = "", str = "0000";
for (; inttmp.length > 3;) {
var temp = new String (inttmp/1000);
if (Temp.indexof (".") = = 1) {
TMP = "," + tmp;
inttmp = temp;
}
else {
var le = new String (temp). Split (".") [1].length;
TMP = "," + new String (temp). Split (".") [1] + str.substring (0, 3-le) + tmp;
inttmp = new String (temp). Split (".") [0];
}
}
inttmp = inttmp + tmp;
Obj.value = Neg = = 0? "-" + inttmp + runing (floattmp): Inttmp + runing (floattmp);
}
= = = To organize a small number of parts
function Runing (val) {
if (Val!= "-1" && val!= "") {
var valvalue = 0 + "." + Val;
if (val.length >= 2) {
Valvalue = parsefloat (valvalue). toFixed (2);
}
var temp = "." + Valvalue.split (".") [1];
return temp;
}
else if (Val!= "0" && val = = "") {
Return ".";
}
else {
Return "";
}
}
At the same time because the amount can be entered negative, so add "neg = Objvalue.indexof ("-")" judgment.
About the amount of formatting problems, often encountered this kind of thing, think this can also be kept, for later access convenience!