Solved

Java Script Decimal Validation

Posted on 2007-03-19
7
4,788 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now