poe Posted August 23, 2008 Share Posted August 23, 2008 how do i validate valid currency from a form the form can either be like: 24.75 (no dollar sign, dollars & cents) $24.75 (with a dollar sign) $24 (with dollar sign, no cents) 24 (no dollar sign, no cents) thanks chris Quote Link to comment https://forums.phpfreaks.com/topic/121016-regex-currency/ Share on other sites More sharing options...
nrg_alpha Posted August 24, 2008 Share Posted August 24, 2008 $entry = '24.75, $24.756, $24, 24, $24.75, $25f, $25.67'; $str = explode(',' , $entry); foreach($str as $val){ if(preg_match('#^\$?\d+(\.\d{2})?$#', trim($val), $matches)){ echo $val . '= valid <br />'; } else { echo $val . '= invalid <br />'; } } ouputs: 24.75= valid $24.756= invalid $24= valid 24= valid $24.7= invalid $25fx= invalid $25.67= valid Quote Link to comment https://forums.phpfreaks.com/topic/121016-regex-currency/#findComment-624039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.