//直接寫在屬性
onkeyup="value=value.replace(/[^\d]/g,'')" onblur="this.value=FormatNumberForPoint(this.value,0);" onfocus="this.value=this.value.replace(/\,/g,'');"
//加入千分號並截取到小數點pointlengtht長度
function FormatNumberForPoint(n, pointlength) {
n += "";
var arr = n.split(".");
var re = /(\d{1,3})(?=(\d{3})+$)/g;
var point = (arr.length == 2 ? "." + arr[1] : "");
if (point.length - 1 > pointlength)
point = point.substr(0, pointlength + 1);
return arr[0].replace(re, "$1,") + point;
}
//截取到小數點pointlengtht長度
function FormatNumberForPointV2(n, pointlength) {
n += "";
var arr = n.split(".");
var re = /(\d{1,3})(?=(\d{3})+$)/g;
var point = (arr.length == 2 ? "." + arr[1] : "");
if (point.length - 1 > pointlength)
point = point.substr(0, pointlength + 1);
return arr[0] + point;
}
--------------------------------------------------------------------------------------
//寫在js or script
//屬性加入 onchange="o_FormatCurrency(this);" onkeypress="inputNubmerPoint();"
//去除"000100" 前端的"000"
//功能:限制只能輸入金額格式
function o_FormatCurrency(obj) {
obj.value = obj.value.replace(/[^0-9]/g, '');
if (obj.value != "") {
obj.value = FormatCurrency(obj.value * 1, '0', '3');
}
}
//輸入控制,僅限輸入 0~9 十個字元和","
//調用實例: this.TextBox1.Attributes.Add("onkeypress", "inputNubmerPoint()");
function inputNubmerPoint() {
var key = window.event.keyCode;
if ((key >= 48 && key <= 57) || key == 44) {
return true;
}
else {
window.event.keyCode = 0;
return true;
}
}
//功能:限制只能輸入金額格式(包含正負)
function o_FormatCurrencyv2(obj) {
var IsNegative = obj.value.indexOf('-');
if (IsNegative != -1 && IsNegative != 0) {
obj.value = "";
}
else {
obj.value = obj.value.replace(/[^0-9]/g, '');
var isNUM = isNaN(obj.value);
if (isNUM == false) {
if (IsNegative == 0)
IsNegative = "-";
else
IsNegative = "";
obj.value = IsNegative + obj.value;
}
else {
obj.value = "";
}
}
}
//輸入控制,僅限輸入 0~9 十個字元和"-"
function inputNubmerPointv2() {
var key = window.event.keyCode;
if ((key >= 48 && key <= 57) || key == 45) {
return true;
}
else {
window.event.keyCode = 0;
return true;
}
}
//輸入控制,僅限輸入 0~9 十個字元
function inputNubmerPointv3() {
var key = window.event.keyCode;
if ((key >= 48 && key <= 57)) {
return true;
}
else {
window.event.keyCode = 0;
return true;
}
}
//格式化數字的函數FormatCurrency
//入參:1. srcStr->被格式化的數字
// 2. nAfterDot->小數位數
// 3. intClause->分隔位數
//調用實例:FormatCurrency('123456789','2','3') ; return:123,456,789.00
function FormatCurrency(srcStr, nAfterDot, intClause) {
var flag = false;
srcStr = srcStr + '';
if (srcStr.indexOf('-') != -1) {
srcStr = srcStr.substring(1);
flag = true;
}
if (!isNaN(srcStr)) {
if (intClause > 0 || intClause <= srcStr.length) {
var IvarA;
var IvarB;
if (srcStr.indexOf('.') != -1) {
IvarA = srcStr.substr(0, srcStr.indexOf('.'));
IvarB = '0' + srcStr.substr(srcStr.indexOf('.'));
}
else {
IvarA = srcStr;
IvarB = "";
}
var Icount = parseInt(IvarA.length / intClause);
var Imod = IvarA.length % intClause;
var newstr;
if (Imod > 0) {
newstr = IvarA.substr(0, Imod) + ',';
}
else {
newstr = '';
}
var format = ',';
for (var i = 1; i <= Icount; i++) {
if (i == Icount) {
format = '';
}
newstr += IvarA.substr(Imod + (i - 1) * intClause, intClause) + format;
}
var Point;
if (IvarB == '') {
if (nAfterDot > 0) {
Point = Math.round(Math.pow(10, nAfterDot));
Point = '0.' + Point.toString().substr(1);
}
else {
Point = '';
}
}
else {
Point = Math.round(IvarB * (Math.pow(10, nAfterDot))) / Math.pow(10, nAfterDot)
}
newstr = newstr + Point.toString().substr(1);
}
if (intClause > srcStr.length) {
newstr = srcStr;
}
}
if (flag) {
newstr = '-' + newstr;
}
return newstr;
}