Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency Form Validation

Status
Not open for further replies.

jasonhuibers

Programmer
Sep 12, 2005
290
CA
I have two text boxes:

Form1.txtAllowance
Form1.txtDebt

When I click on the submit button I want to validate that the values in txtAllowance and txtDebt contain only numbers, ',' or '.'

Can someone provide me working example using a submit button and the two text boxes?

Help is greatly appreciated!
 
Something simple and straightforward like this:

Code:
[COLOR=#990000 ]<[/color]script type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"text/javascript"[/color][COLOR=#990000 ]>[/color]
[b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]checkforCurrency[/color][/b][COLOR=#990000 ]([/color]formObj[COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab]  [b][COLOR=#0000FF ]var[/color][/b] areCurrency[COLOR=#990000 ]=[/color][COLOR=#993399 ]0[/color][COLOR=#990000 ];[/color]
[tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]isCurrency[/color][/b][COLOR=#990000 ]([/color]formObj[COLOR=#990000 ].[/color]txtAllowance[COLOR=#990000 ].[/color]value[COLOR=#990000 ]))[/color]
[tab][COLOR=#FF0000 ]{[/color]   
[tab][tab]areCurrency[COLOR=#990000 ]++;[/color]
[tab][COLOR=#FF0000 ]}[/color]
[tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]isCurrency[/color][/b][COLOR=#990000 ]([/color]formObj[COLOR=#990000 ].[/color]txtDebt[COLOR=#990000 ].[/color]value[COLOR=#990000 ]))[/color]
[tab][tab][COLOR=#FF0000 ]{[/color]
[tab][tab][tab]areCurrency[COLOR=#990000 ]++;[/color]
[tab][tab][COLOR=#FF0000 ]}[/color]
[tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color]areCurrency[COLOR=#990000 ]<[/color][COLOR=#993399 ]2[/color][COLOR=#990000 ])[/color]
[tab][tab][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#000000 ]alert[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]"One of the values is not valid currency"[/color][COLOR=#990000 ]);[/color]
[tab][tab] [b][COLOR=#0000FF ]return[/color][/b] false[COLOR=#990000 ];[/color]
[tab][COLOR=#FF0000 ]}[/color]
[tab][tab][b][COLOR=#0000FF ]else[/color][/b]
[tab][tab][COLOR=#FF0000 ]{[/color]
[tab][tab]  [b][COLOR=#0000FF ]return[/color][/b] true[COLOR=#990000 ];[/color]  
[tab][tab][COLOR=#FF0000 ]}[/color]
[tab]  [b][COLOR=#0000FF ]return[/color][/b] false[COLOR=#990000 ];[/color]
[COLOR=#FF0000 ]}[/color]
[tab]
  [b][COLOR=#0000FF ]var[/color][/b] isCurrency_re[tab][COLOR=#990000 ]=[/color] [COLOR=#990000 ]/^(?:\[/color]d[COLOR=#990000 ]+(?:,\[/color]d[COLOR=#FF0000 ]{[/color][COLOR=#993399 ]3[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])*(?:\.\[/color]d[COLOR=#FF0000 ]{[/color][COLOR=#993399 ]2[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])?)[/color]$[COLOR=#990000 ]/;[/color]

[b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]isCurrency[/color][/b] [COLOR=#990000 ]([/color]s[COLOR=#990000 ])[/color] 
[COLOR=#FF0000 ]{[/color]

   [b][COLOR=#0000FF ]return[/color][/b] [b][COLOR=#000000 ]String[/color][/b][COLOR=#990000 ]([/color]s[COLOR=#990000 ]).[/color][b][COLOR=#000000 ]search[/color][/b] [COLOR=#990000 ]([/color]isCurrency_re[COLOR=#990000 ])[/color] [COLOR=#990000 ]!=[/color] [COLOR=#990000 ]-[/color][COLOR=#993399 ]1[/color]

[COLOR=#FF0000 ]}[/color]

[tab][COLOR=#990000 ]</[/color]script[COLOR=#990000 ]>[/color]

[COLOR=#990000 ]...[/color]

[COLOR=#990000 ]<[/color]form [COLOR=#990000 ]...>[/color]
[COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"text"[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"txtAllowance"[/color][COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"text"[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"txtDebt"[/color][COLOR=#990000 ]>[/color]

[COLOR=#990000 ]<[/color]input type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"submit"[/color] [COLOR=#990000 ]...[/color] onclick[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]" return checkforCurrency(this.form);"[/color][COLOR=#990000 ]>[/color]
[COLOR=#990000 ]</[/color]form[COLOR=#990000 ]>[/color]

For an explanation of the regular expression used go here:


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
That regex isn't perfect; entering only 1 digit after the DP causes it to regard the value as non-currency, when that isn't the case:

Code:
alert(1000.6 === 1000.60);  // true

Changing [tt]d{2}[/tt] to [tt]d{1,2}[/tt] fixes this.

It also fails to allow negative numbers... which would also be valid currency values (even though the OP didn't mention the '-' character as one he wanted to allow).

Dan


Coedit Limited - Delivering standards compliant, accessible web solutions

[blue]@[/blue] Code Couch:
[blue]@[/blue] Twitter:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top