Click to See Complete Forum and Search --> : Eliminating decimals
tolearn
03-07-2003, 04:10 PM
I have an HTML program that I have written, that has two input fields in it. One of the Input fields allows the user to enter in a Total Quantity. Once they enter data in the two Input fields, the program takes them to a second page.
What I am trying to do, is create a Javascript function that will take out the decimal points if accidently entered. I have tried the isNaN function and it does not do anything for this situation.
Here is what I have so far:
if (isNaN(document.CoreRecvd.totqty.value)) {
window.alert("Total Quantity must be Numeric.");
CoreRecvd.totqty.focus();
return false;
}
return true;
}
Can anyone please tell me how to get rid of decimals if accidently entered?
I would greatly appreciate it!
Yah, use RegExps and replace().
Something like this....
var re = /\./g;
var box = document.formName.textFieldName.value
if(re.exec(box)){box=box.replace(re, ""); }
tolearn
03-07-2003, 06:26 PM
Thank you Jona and Dave, for your replies.
I will try those methods and see if I can get them to work.
Dave,
To answer your question...yes validate for. What I meant by saying "eliminate", is that if the user does accidently enter a decimal value in the Total Qty field (10.3), then when they press the Confirm button that takes them to the second page (last page to verify the entry before it writes to the database), the Total Qty value shown on the second page is (10).
Thanks.
tolearn
03-07-2003, 07:06 PM
Dave,
I tried your example and it did not work. I entered it exactly as you stated in your reply, but it still allowed for decimal values.
Any other ideas?
khalidali63
03-07-2003, 07:13 PM
Here you go this will work
<script type="text/javascript">
function blockDecimal(obj){
var val = obj.value;
if(val.indexOf(".")>-1){
val = val.replace(".","")
obj.value = val;
}
}
</script>
</head>
<body class="body">
<form name="form1" action="" onsubmit="">
<input type="Text" name="t1" onkeyup="blockDecimal(this)"></input>
</form>
Cheers
Khalid
That's basically what I said, Khalid. I just... didn't do a whole page for it. :) Good job, though.
Dan Drillich
03-08-2003, 06:34 PM
I would try to catch the ‘wrong’ character as it’s being typed. Like -
<script>
function checkInput(v) {
if (v.value.indexOf('.') >= 0) alert("Please avoid using decimal numbers!");
}
</script>
<form>
<input type=text name=test size=12 onkeyup="checkInput(this)">
</form>
khalidali63
03-08-2003, 06:39 PM
Originally posted by Dan Drillich
I would try to catch the ‘wrong’ character as it’s being typed. Like -
:D
Thats exactly what I posted couple posts above..
Cheers
Khalid
Dan Drillich
03-08-2003, 07:45 PM
You are right! :)