How do i format a text box to hold float numbers in format 12.12

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spooky
    New Member
    • Oct 2006
    • 27

    How do i format a text box to hold float numbers in format 12.12

    Hi,

    I have a text box that holds floating numbers how do i format it so that the user will not be able to enter values greater than 99.99.

    I want to insert a '.' when the length of the textbox is 2.

    i tried to do it this way but while clearing the text using back space button it showed problems
    //struts text field//


    html:text styleId="txtId" property="txt" onkeyup="countC har(this.id)" maxlength="5"

    //script code//

    [CODE=javascript]function countChar(obj)
    if(document.get ElementById(obj ).value.length= =2)
    document.getEle mentById(obj).v alue=document.g etElementById(o bj).value+'.';
    [/CODE]



    thanks
    Anil
    Last edited by acoder; Feb 13 '08, 08:50 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Instead of checking onkeyup, just check when the form is submitted.

    If you want to check onkeyup, you will need to check that the first two characters are numbers and that there is not already a dot in place. I go for simplicity - just use a regular expression for checking float numbers.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Half of your problem is solved...
      Fix the remaining yourself...

      [HTML]<script type="text/javascript">
      function format99Dot99(i np) {
      var valid = 0;
      nums = new Array('1','2',' 3','4','5','6', '7','8','9','0' ,'.');
      var v = '';
      var x = '';
      for (var i=0; i<inp.value.len gth; i++) {
      x = inp.value.charA t(i);
      for (var j=0; j<=10; j++)
      if (x==nums[j]) valid = 1;
      if (valid) v += x;
      valid = 0;
      }
      //if (v>=10) v+='.';
      if (v>99.99)
      while (v > 99.99)
      v = v%100;
      inp.value = v;
      }
      </script>
      <input type="text" onkeyup="format 99Dot99(this)" maxlength=5>[/HTML]

      You can use the code on line 13, but it won't let the user delete the dot once added, as you already tried the similar.
      Last edited by hsriat; Feb 13 '08, 09:20 AM. Reason: missed resetting valid to 0

      Comment

      • spooky
        New Member
        • Oct 2006
        • 27

        #4
        Originally posted by acoder
        Instead of checking onkeyup, just check when the form is submitted.

        If you want to check onkeyup, you will need to check that the first two characters are numbers and that there is not already a dot in place. I go for simplicity - just use a regular expression for checking float numbers.

        Hi,

        Am not that good in javascript. Can you sho me how it can be done using the reg expression.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by spooky
          Hi,

          Am not that good in javascript. Can you sho me how it can be done using the reg expression.
          In this case, you need something like:
          [code=javascript]/^\d{2}\.\d{2}$/[/code] See this link which shows you how to use regular expressions in JavaScript.

          Comment

          • spooky
            New Member
            • Oct 2006
            • 27

            #6
            Originally posted by acoder
            In this case, you need something like:
            [code=javascript]/^\d{2}\.\d{2}$/[/code] See this link which shows you how to use regular expressions in JavaScript.

            Hi,

            Thanks for the reply but will this be dynamic, I have a text field for interest so the values the user enters must be within 99.99. how do i implement the expression you have given. i went through the site link you gave and the live example in it. there it was not dynamic implementation ... i hope you can understand. I have seen sites where when i enter my date of birth the '-' comes in between by itself thus avoiding some level of validation. I want to do the same

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              See this example - you can adapt it to your requirements.

              Comment

              Working...