Solved

Java Script Decimal Validation

Posted on 2007-03-19
7
4,792 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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