homepage Welcome to WebmasterWorld Guest from 54.167.75.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
formating decimal in javascript
manjumurthy




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

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

 

txbakers




msg:1479290
 5:28 pm on May 31, 2002 (gmt 0)

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?

jatar_k




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

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.

volatilegx




msg:1479292
 6:27 pm on May 31, 2002 (gmt 0)

JavaScript doesn't handle decimals very well. I also recommend using whole numbers, and inserting any decimal points after any calculations are done.

txbakers




msg:1479293
 6:54 pm on May 31, 2002 (gmt 0)

There are some good VBscript functions that do handle decimal formatting. Why not use one of these just for the decimal formatting.

DrDoc




msg:1479294
 12:22 am on Jun 1, 2002 (gmt 0)

>> 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.

manjumurthy




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

Thanks for the function DrDoc. It works fine in I.E but on netscape its behaviour is different. Can u tell me y??

DrDoc




msg:1479296
 6:18 pm on Jun 1, 2002 (gmt 0)

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 :)

DrDoc




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

<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>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved