Solved

Java Script Decimal Validation

Posted on 2007-03-19
7
4,780 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
Comment Utility
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
Comment Utility
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
Comment Utility
we will see :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:judsonmusic
Comment Utility
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
Comment Utility
Yes I can.
0
 
LVL 3

Author Comment

by:judsonmusic
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

17 Experts available now in Live!

Get 1:1 Help Now