Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
^(\$(?=[1-9])((\d*\.\d{1,2})|\d+\.?)|((?=[1-9])(\d+\.?)|((?=([1-9]+|\d*\.0*[1-9]+))\d*\.\d{1,4}))\%)$
/

Description

This will verify input is non-zero and either a valid dollar amount ($3, $3.4, $3.40, $.4, $.40) or a valid percent/rate (2%, 2.5%, .5%) with up to four decimal places. Could probably be more efficient with the lookaheads.

Submitted by Keith Freeman - 9 years ago