Solved

Java Script Decimal Validation

Posted on 2007-03-19
7
4,794 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

856 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