/    Sign up×
Community /Pin to ProfileBookmark

Regular expression for a floating number

Hi all Im new to regular expressions so please forgive me. I have a field which is a price of a particular object and I would like to validate that the value is OK. Obviously a price can look like this

45,000.00

what I would like it do is validate that the value has been entered like the above. Is there any examples out there that could do this. I have googled regular expression + floating number + php and various combinations of this and to no avail. I did find something along the following lines

[code=php]if (!(eregi(‘b[0-9]{1,3}(?:,?[0-9]{3})*(?:.[0-9]{2})?b’, stripslashes(trim($_POST[‘price’]))) )) {
$errors[] = ‘<font color=”white”>The overall Price of the Land Investment</font>’;
} [/code]

but this doesnt seem to work.

regards

Mike

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 01.2008 — preg* are recommended over ereg*, so maybe something like this:<i>
</i>preg_match("/^d{0,3}(,d{3})*(.d+)?$/",$str);
That presumes there is no white space before or after. For white space before or after the price, use:<i>
</i>preg_match("/^bd{0,3}(,d{3})*(.d+)?b$/",$str);
and if you want to exclude any currency symbol infront of it:<i>
</i>preg_match("/^[^wds]?d{0,3}(,d{3})*(.d+)?$/",$str);
×

Success!

Help @cs3mw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...