Click to See Complete Forum and Search --> : amount format validation


sweet_sentiment
05-04-2004, 06:34 AM
say i have this input type field:

<input name="criteriaPaymentAmount" value="" onBlur="validateAmount()">


do you know any javascript code that validates an amount format that would do the following?


>would only accept 1 decimal point
>commas would be omitted and would display a new value without the commas
>checks whether the user has indeed entered a number and not some alpha/special characters


thanks in advance!

Khalid Ali
05-04-2004, 12:41 PM
the following is not the best code,but its a quick solution....:D

<script type="text/javascript">
<!--
String.prototype.RoundIt=function(){
var temp = this;
temp = temp.replace(/,/g,'');
var x=0;
if(temp.indexOf(".")>-1){
var t1 = temp.substring(temp.indexOf(".")+1,temp.length);
for(var n=t1.length;n>=0;n--){
var m= Number(t1.charAt(n));
if(m>5){
x= Number(t1.charAt(n-1))+1;
}
}
x = temp.substring(0,temp.indexOf(".")+1)+x;
}
return x;
}
function validateAmount(obj){
obj.value = obj.value.replace(/,/g,'');
if(Number(obj.value)){
var val = obj.value.RoundIt();
alert(val)
}else{
alert("Value = "+obj.value+" is not a number")
}
return true;
}
//-->
</script>
</head>

<body>
<form id="form1" action="" onsubmit="">
<input name="criteriaPaymentAmount" value="1,222,223.369" onBlur="return validateAmount(this);">
</form>

sweet_sentiment
05-05-2004, 02:14 AM
@ Khalid Ali


what do you mean by it's not the best solution? what problems will i encounter when i used the codes....

but hey, it works! i just want to know the downside of the code above..

thanks!