php - How to create regex with following conditions? -


i have regexp dollar validation using regexp in javascript.

/^\$?\d{1,3}(,?\d{3})*\.?(\d+)?$/ 

but doesn't work $.01 greater 0 (as dollar sign optional) , valid amount. requirements are-

1.$2000 2.2000 3.$2000.00 4.$2,000 5.2000.00 6.2,000.00 7.$.04 

please suggest changes in current regexp.

thanks john

found solution::--

^\$?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$ 

following regex should work you:

^\$?(?=.)(?:\d{1,3})?(?:,?\d{3})*(?:\.\d+)?$ 

live demo: http://www.rubular.com/r/0qqbwtrmol

just remember make digits before dot optional match , enforces use of positive lookahead make sure there @ least 1 digit after optional $ sign.

here non-regex based solution validate currency (usd):

$fmt = new numberformatter( 'en-us', numberformatter::currency ); #$num = "1.234.567,89 $"; $num = "$.04";  var_dump($fmt->parsecurrency($num, $curr)); // returns float value: float(0.04) var_dump($curr); // returns string representing currency: string(3) "usd" 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -