Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Java Script Decimal Validation

Posted on 2007-03-19
Medium Priority
4,800 Views
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
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
• 4
• 2

LVL 63

Expert Comment

ID: 18749861
First part is this:

<html>
<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>
<body>
<form>
<input type="text" name="myDecimal" onKeyUp="checkDec(this)" >
</form>
</body>
</html>

Second part I do not understand.

0

LVL 11

Expert Comment

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

ID: 18752212
we will see :)
0

LVL 3

Author Comment

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

ID: 18835863
Yes I can.
0

LVL 3

Author Comment

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

Zvonko earned 2000 total points
ID: 18835938
Like this:

<html>
<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>
<body>
<form>
<input type="text" name="myDecimal" onKeyUp="checkAndSetDec(this)" >
</form>
</body>
</html>

0

## Featured Post

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll