Click to See Complete Forum and Search --> : validate form price


illusime
10-11-2003, 05:09 AM
I have created a textfield to enter the price of a product they want to sell. How do i validate that it is a validate price using javascript?

anyone has the code?

Charles
10-11-2003, 06:52 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>

<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>

<script type="text/javascript">
<!--
String.prototype.reverse = function () {return this.split('').reverse().join('')};

function Dollars (d) {this.ammount = typeof d == 'number' ? d : Number(d.toString().replace(/[$,]/g, ''))};

Dollars.prototype.valueOf = function () {return this.ammount};

Dollars.prototype.toString = function () {
if (isNaN (this.ammount)) return NaN.toString();
var l = Math.floor(Math.abs(this.ammount)).toString();
var r = Math.round((Math.abs(this.ammount) % 1) * 100).toString();
return [(this.ammount < 0 ? '-' : ''), '$', (l.length > 4 ? l.reverse().match(/\d{1,3}/g).join(',').reverse() : l),'.', (r < 10 ? '0' + r : r)].join('');
}
// -->
</script>

<form action="">
<div>
<label>Price<input type="text" onchange="this.value = new Dollars(this.value)"></label>
<button type="submit">Submit</button>
</div>
</form>

illusime
10-11-2003, 08:38 AM
thanks it helps alot~