Removing Numbers

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Removing Numbers

Post by SheDesigns »

I'm making a script that calculates price in JS.

It won't calculate the total right if they put a "$" or "," in the field. Decimals are allowed.

Please help!! I need it to remove other characters that aren't an number or a decimal. All the scripts I've been trying online .. none of them work. :(
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Removing Numbers

Post by susrisha »

dont know if it will be of much help .. http://www.jsmadeeasy.com/javascripts/c ... t_test.asp
http://javascript.internet.com/forms/money-filter.html

and if i am guessing right, you have input fields which you want the user to enter even $ and , into. well then let me try my own. will let you know if that has worked .. ;)
Last edited by susrisha on Fri Mar 13, 2009 10:03 am, edited 1 time in total.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Removing Numbers

Post by susrisha »

I have attached a rough code of summing up two values.. thought might be useful :)

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function calculate()
{
    var ele1 = document.getElementById("money1").value;
    var ele2 = document.getElementById("money2").value;
    //var parsedele1 = ele1;
    //var isDollar = false;
    // String parsed = new String();
    var ele3 = document.getElementById("money1");
    var sometext = ele3.toString();
    //sometext.substring();
//  sometext.length
    sometext.replace(",","");
    //parsed.
    if(ele1.charAt(0)=="$")
    {
        ele1= ele1.substring(1,ele1.length);
        ele1.replace(",","");
    //  alert("dollar");
    }
 
    if(ele2.charAt(0)=="$")
    {
        ele2= ele2.substring(1,ele2.length);
        //ele2.replace(",","");
    //  alert("dollar");
    }
    ele1  = ele1.replace(",","");
   ele2 =  ele2.replace(",","");
    
    
    
    //alert("ele1 "+ele1.value.toString());
    var sum = Number(ele1)+Number(ele2);//ele1.Value + ele2.Value;
    
    alert("sum is "+sum);
    document.getElementById("result").value = sum.toString();
    return false;
}
</script>
</head>
<body>
<form action="#" method="POST" onsubmit="return calculate();" >
<ul>
<li>
<input type="text" name="money1" id="money1" />
</li>
<li>
<input type="text" name="money2" id="money2"/>
</li>
<li>
<input type="text" name="result" id="result"/>
</li>
</ul>
<input type="submit" name="none" value="add" />
</form>
 
</body>
</html>
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Removing Numbers

Post by pickle »

A regex would work, or you could check character codes as they're typed. Regex would probably work better.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Re: Removing Numbers

Post by SheDesigns »

Thank you for all of your help.. I've almost found a solution.

Code: Select all

function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
         return true;
      }

But I need that to also allow for a decimal, and a decimal only! The char code is 190, but it won't work when I add it in the if statement...?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Removing Numbers

Post by crazycoders »

SheDesigns, i suggest not to use this method because when you paste data, it doesn't fire the keypress, keyup or keydown event.

I have that problem in another web app where we wanted only numbers and - sign. When we submitted the info, we'd take into account the value was fine while it wasn't...

Test it out, copy something invalid and paste it in...

et voila!
Post Reply