Solved

Java Script Decimal Validation

Posted on 2007-03-19
7
4,796 Views
Last Modified: 2012-05-11
I need a javascript that will force a decimal after a user inserts 3 numbers into a textfield and show the decimal in the textfeild on form load.

Thanks'

Judson
0
Comment
Question by:judsonmusic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 18749861
First part is this:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function checkDec(theField){
  var theVal = theField.value.replace(/\D/g,'');
  if(theVal.length>2){
    theVal = theVal.substr(0,3)+"."+theVal.substr(3);
    if(theVal!=theField.value){
      theField.value = theVal;
    }
  }
}
</script>
</head>
<body>
<form>
<input type="text" name="myDecimal" onKeyUp="checkDec(this)" >
</form>
</body>
</html>


Second part I do not understand.

0
 
LVL 11

Expert Comment

by:alexcohn
ID: 18750539
Zvonko, your solution satisfies the second requirement, as I read it. The point is that the decimal point be part of the text (at least, on Submit) and not simply some visual effect. Your solution forces numeric input and ensures that a point is part of the text as long as it is longer that two digits. I believe this is a complete solution.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18752212
we will see :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:judsonmusic
ID: 18835783
Zvonko,

Can you explain to me exactly which each line of this does?

function checkDec(theField){
  var theVal = theField.value.replace(/\D/g,'');
  if(theVal.length>2){
    theVal = theVal.substr(0,3)+"."+theVal.substr(3);
    if(theVal!=theField.value){
      theField.value = theVal;
    }
  }
}

Thanks,

Judson


0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18835863
Yes I can.
0
 
LVL 3

Author Comment

by:judsonmusic
ID: 18835869
Ok, one problem, is there a way that if someone want to put a vlue of "4.2" for wxample that it will either place two 0's in front of the 4 or override it all together?


Thanks


Judson
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 18835938
Like this:

<html>
<head>
<title>Zvonko &#42;</title>
<script>
function checkAndSetDec(theField){
  var thePos = theField.value.indexOf(".");
  var theVal = theField.value.replace(/\D/g,'');
  if(theVal.length>2 || thePos>0){
    if(thePos==1) theVal = "00"+theVal;
    if(thePos==2) theVal = "0"+theVal;
    theVal = theVal.substr(0,3)+"."+theVal.substr(3);
    if(theVal!=theField.value){
      theField.value = theVal;
    }
  }
}
</script>
</head>
<body>
<form>
<input type="text" name="myDecimal" onKeyUp="checkAndSetDec(this)" >
</form>
</body>
</html>


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question