Javascript REmove all but numerics and periods

Is there a javascript rule that removes everything but
numerics one period and two numerics after the period?

so -

£10,000.00 + additional

or

£10000.0078787

or

$10,000

all become

either 10000.00 or 10000

Thanks

garethtnashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
May help : http://jsfiddle.net/ehLNv/
var str = "£10,000.00  + additional".replace(/[^\d\.]/g,"");
alert(parseFloat(str).toFixed(2));

str = "£10000.0078787".replace(/[^\d\.]/g,"");
alert(parseFloat(str).toFixed(2));

str = "$10,000".replace(/[^\d\.]/g,"");
alert(parseFloat(str).toFixed(2));

Open in new window

0
garethtnashAuthor Commented:
Hi, Looks good :)

Is there anyway of doing it as a function that can be called onblur?
So when the cursor leaves the checkbox, the figure is corrected? whatever the figure is, be it £10,,000 or $1.24?

Thank you
0
leakim971PluritechnicianCommented:
function formatField(field) {
     return isNaN(field.value)?0:(parseFloat(field.value.replace(/[^\d\.]/g,"")).toFixed(2));
}

<input onblur="this.value = formatField(this)"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

garethtnashAuthor Commented:
Hello,

Have tried the following -

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function formatField(field) {
     return isNaN(field.value)?0:(parseFloat(field.value.replace(/[^\d\.]/g,"")).toFixed(2)); 
}
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="newprodust" id="newprodust">
  <input name="textfield" type="text" id="textfield" onblur="this.value = formatField(this)" />
</form>
</body>
</html>

Open in new window


But sadly it doesn't work...

If I enter £34.00 on blur it changes to 0
if i enter 34.00000 on blur it changes to 34.00 (great)
if i enter 34,000.000 on blur I get 0
If I enter 34000 plus 20% on blur i get 0
If I enter nothing on blur I get NaN..

Appreciate your help --

Thank you
0
leakim971PluritechnicianCommented:
check this one : http://jsfiddle.net/ehLNv/3/
function formatField(field) {
    var v = field.value.replace(/^\s+|\s+$/g, "");
    if(v.length == 0) return "0";
    v = v.split(" ")[0].replace(/[^\d\.]/g,"");
    if(isNaN(v)) return "0";
    return parseFloat(v).toFixed(2); 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garethtnashAuthor Commented:
That will do nicely - thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.