摘要:html的textbox自動產生千分位
function cc(s) {//千分位
var tmpS=s.toString();
var negative = "";
if(s.length>1){
negative = tmpS.substr(0,1);
}
tmpS=tmpS.replace(new RegExp("-","gm"), "");
tmpS=parseFloat(tmpS);//去除首位輸入的0,如002,小數不影響parseFloat字符串
tmpS=tmpS.toString();
tmpS = tmpS.replace(/^(\d*)$/, "$1.");
tmpS = tmpS.replace(".", ",");
var re = /(\d)(\d{3},)/;
while (re.test(tmpS))
tmpS = tmpS.replace(re, "$1,$2");
tmpS = tmpS.replace(/,(\d\d)$/, ".$1");
if("-"==negative){
return "-"+tmpS.replace(/^\./, "0.");
}else{
return tmpS.replace(/^\./, "0.");
}
}
var tmpNonCommaNum="";
var tmp1="";
function comma(e){
tmpNonCommaNum=delComma($(e).val());
if( tmpNonCommaNum.length>3 ){
tmp1=cc(tmpNonCommaNum);
tmp1=tmp1.substr(0,tmp1.length-1);
if(tmp1.substr(0,1)==","){
tmp1=tmp1.substr(1,tmp1.length-1);
}
return $(e).val(tmp1);
} else{ //未滿三位數,千分位也要去掉","
return $(e).val(tmpNonCommaNum);
}
}
angularJS的controller:
$scope.isSaveHide="display:none";
$scope.isTextBoxHide="display:none";
$scope.isLableHide="display:''";
<td><label data-ng-model="alc1" style={{isLableHide}}></label>
<input ng-keyup="calc()" class="form-control" data-ng-model="alc1" type="text" id="alc1" name="alc1" onkeyup="comma($(this),value)" style="width:1000px;{{isTextBoxHide}}"/></td>