ITeye博客

金额只为大写且只保留两位小数

jh_soft · 2009-08-06 11:03 · Markdown

function NumericKeyUp(p,s){ if(p == 0){ return; } var intLen = p - s ; var re = /[^0-9.]/; var srcObj = event.srcElement; if(srcObj && srcObj.type == 'text'){ var text = srcObj.value; var reg = new RegExp(re ); var changed = false; while(reg.test(text)){ changed = true; text = text.replace(reg , ""); }

	re = /^.*\..*(\.).*$/;
	reg = new RegExp(re );
	while(reg.test(text)){
		changed = true;
		text = text.replace(RegExp.$1 , "");
	}
	
	if(! validateNumeric(text,p,s)){
		
		if(text.indexOf(".") == -1){
				text = (parseInt(text) + "").substr(0,intLen);
		}else{
				var arr = text.split(".");
				if(arr.length >=2){
					
					var intValue = arr[0];
					if(intValue.length > intLen){
	
							intValue = intValue.substr(0,intLen);
					}
					var floatValue = arr[1];
					if(floatValue.length > s){
						floatValue = floatValue.substr(0,s);
					}
					text = intValue + "." + floatValue ;
			}
				
		}
		changed = true;
}
	
	if(changed){
		srcObj.value = text;
		srcObj.select();	
	}
}
return true;

}

调用:onkey="NumericKeyUp(11,2);" onkeypress="NumericKeyPress(11,2);"

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。