/    Sign up×
Community /Pin to ProfileBookmark

Extract Number from String

Hi All,

How would I go about extracting a number from a string value. I have a form in which users can enter monitary values. I have no idea what they will enter so I want to extract the number including decimal from whatever they enter. For example if the user enters $134,876.00 I want to extract 134876.00.

I am trying to use a regular expression to accomlish this but can’t seem to get it to return a single number. It returns an array if the number is broken up in the middle.

[code]var x = 123adv456.8;
var r = x.match(/[0-9.]+/g);[/code]

Thanks in advance for any help.

_t

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@paulnicJan 15.2009 — why dont you add some form validation instead which forces the user to enter just numbers?
Copy linkTweet thisAlerts:
@scragarJan 15.2009 — <i>
</i>var x = '123adv456.8';
var r = x.replace(/[^0-9\.]/g, '').replace(/^(d*.d*)..*$/, "$1") * 1.0;
Copy linkTweet thisAlerts:
@shotsy247authorJan 15.2009 — Works great Scragar. Thanks!

_t
×

Success!

Help @shotsy247 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...