Welcome to WebmasterWorld Guest from 54.198.69.193

Forum Moderators: open

Message Too Old, No Replies

formating decimal in javascript

     
7:44 am on May 31, 2002 (gmt 0)

10+ Year Member



I want a decimal to appear after i type 9 digits in the input field, & I sould be able to enter 2 digits after the decimal. Can anybody send nme a javascript validation for this
Thanks in advance
5:28 pm on May 31, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I would convert the entry to string, then do a left split after 9 digits, concatenate a decimal, then bring in the other two digits.

Is this what you are after?

5:35 pm on May 31, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



or have two text boxes with a text period in the middle one that is maxlength=9 and one that is maxlength=2 and cat them after with a period in the middle.
6:27 pm on May 31, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript doesn't handle decimals very well. I also recommend using whole numbers, and inserting any decimal points after any calculations are done.
6:54 pm on May 31, 2002 (gmt 0)

WebmasterWorld Senior Member txbakers is a WebmasterWorld Top Contributor of All Time 10+ Year Member



There are some good VBscript functions that do handle decimal formatting. Why not use one of these just for the decimal formatting.
12:22 am on Jun 1, 2002 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



>> JavaScript doesn't handle decimals very well.

I disagree! What makes you say that? JavaScript handles decimals just fine, as long as you know how to write the functions.

manjumurthy, should the decimal appear automatically after 9 digits?

Provided that the name of the form is "myForm", and the text field is "myField", this script should work cross-browser. If not, just replace with the actual names.

<script language="javascript" type="text/javascript">
function myFunc() {
theValue = document.myForm.myField.value;
rx = /[^0-9.]/;
if(rx.test(theValue)) {
alert("The field can only contain numbers");
return;
}
if(theValue.indexOf(".") != -1) {
theValue = theValue.substring(0,(theValue.indexOf(".") + 3));
}
lnt = theValue.length;
if(lnt > 11) {
if(theValue.indexOf(".") == -1) {
theValue = theValue.substring(0,11);
}
else {
theValue = theValue.substring(0,12);
}
lnt = theValue.length;
}
if(lnt > 9 && theValue.indexOf(".") == -1) {
first = theValue.substring(0,9);
second = theValue.substring(9);
theValue = first + "." + second;
}
document.myForm.myField.value = theValue;
}
</script>

<input type="text" maxlength="12" name="myField" onkeyup="myFunc()" />

Max 2 decimals
Max 11 digits (including decimals), plus period.

8:58 am on Jun 1, 2002 (gmt 0)

10+ Year Member



Thanks for the function DrDoc. It works fine in I.E but on netscape its behaviour is different. Can u tell me y??
6:18 pm on Jun 1, 2002 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Are you talking about NN4, manjumurthy?

I noticed that too .. Plus it has some other queer behaviors .. I'll post an updated one that works a little bit better ;)

I just posted it so I would know if that's what you were asking for or not .. And now I know :)

8:43 pm on Jun 1, 2002 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



<script language="javascript" type="text/javascript">
NN4 = (document.layers ? true : false);
oldValue = 0;
function myFunc() {
theValue = document.myForm.myField.value;
rx = /[^0-9.]/;
if(rx.test(theValue)) {
alert("The field can only contain numbers");
return;
}
if(theValue.indexOf(".") != -1) {
theValue = theValue.substring(0,(theValue.indexOf(".") + 3));
}
lnt = theValue.length;
if(lnt > 11) {
if(theValue.indexOf(".") == -1) {
theValue = theValue.substring(0,11);
}
else {
theValue = theValue.substring(0,12);
}
lnt = theValue.length;
}
if(lnt > 9 && theValue.indexOf(".") == -1) {
first = theValue.substring(0,9);
second = theValue.substring(9);
theValue = first + "." + second;
}
if(NN4 && document.myForm.myField.value >= 100000000000) {
window.status="a";
theValue = oldValue;
document.myForm.myField.value = theValue;
}
else if(theValue >= 1000000000) {
theValue = oldValue;
}
oldValue = theValue;
if(!NN4) {
document.myForm.myField.value = theValue;
}
}
function NN4Func() {
if(NN4) {
document.myForm.myField.value = theValue;
}
}
</script>

<form name="myForm">
<input type="text" maxlength="12" name="myField" onkeyup="myFunc()" onchange="NN4Func()" />
</form>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month