<script type="text/javascript">
function ismoney(obj) {
check(obj);
}
function check(obj) {
var v = obj.value;
if (v.indexOf(",") > -1) {
//如果出現在最后一位和第一位不正確
if (v.indexOf(",") == 0) {
alert("第一位不能出現,號")
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
return false;
}
if (v.lastIndexOf(",") == (v.length - 1)) {
alert("最后一位不能出現,號");
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
return false;
}
var tmp = v.split(",");
for (var i = 1; i < tmp.length; i++) {
if (i == (tmp.length - 1)) {//最后一位
if (tmp[tmp.length - 1].indexOf(".") > -1) {
var la = tmp[tmp.length - 1].split(".")[0];
if (la == "" || la.length != 3) {
alert("小數點前的數字格式不正確,位數不為3個不能用逗號分隔");
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
return false;
}
} else if (tmp[i] == "" || tmp[i].length != 3) {
alert("位數不為3個不能用逗號分隔");
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
return false;
}
} else if (tmp[i] == "" || tmp[i].length != 3) {
alert("數字位數不正確");
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
return false;
}
}
v = v.replace(/,/g, "");
}
var a = /^[0-9]*(\.[0-9]{1,2})?$/;
if (!a.test(v)) {
alert("格式不正確");
changecolor(obj);
var timeID = setTimeout("changecolor('" + obj.id + "')", 2000);
obj.value = "";
// clearTimeout(timeID);
return false;
} else {
//alert("格式正確");
return true;
}
}
function changecolor(obj) {
if (obj.type != "text")
obj = document.getElementById(obj);
//if(obj.type=="text")
// alert(obj.style.backgroundColor);
if (obj.style.backgroundColor == "red") {
obj.style.backgroundColor = ""
} else {
obj.style.backgroundColor = "red"
}
}
</script>
<asp:TextBox ID="txtZzkj" runat="server" onblur='ismoney(this)'></asp:TextBox>