?
Solved

Javascript REmove all but numerics and periods

Posted on 2011-10-05
6
Medium Priority
?
204 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:garethtnash
  • 3
  • 3
6 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36916336
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
 

Author Comment

by:garethtnash
ID: 36916397
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
 
LVL 83

Expert Comment

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

<input onblur="this.value = formatField(this)"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:garethtnash
ID: 36922805
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
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36924918
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
 

Author Closing Comment

by:garethtnash
ID: 36925078
That will do nicely - thank you
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question