Page 1 of 1
Removing Numbers
Posted: Fri Mar 13, 2009 8:57 am
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.

Re: Removing Numbers
Posted: Fri Mar 13, 2009 9:08 am
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 ..

Re: Removing Numbers
Posted: Fri Mar 13, 2009 9:38 am
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>
Re: Removing Numbers
Posted: Fri Mar 13, 2009 10:09 am
by pickle
A regex would work, or you could check character codes as they're typed. Regex would probably work better.
Re: Removing Numbers
Posted: Fri Mar 13, 2009 10:12 am
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...?
Re: Removing Numbers
Posted: Fri Mar 13, 2009 10:16 am
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!