---
title: "金额只为大写且只保留两位小数"
author: "jh_soft"
date: "2009-08-06T11:03:26Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-08-06T11:03:26Z"
id: 441382
url: "https://jh-soft.iteye.com/blog/441382"
---

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);"
