add decimal at the number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • java
    New Member
    • Oct 2006
    • 10

    add decimal at the number

    hi,

    how to add .00 to a number after the onBlur event?

    For example,if i key in 123 at the textbox, when i press the tab, it will be appear as 123.00.


    thx.
  • Thevercad
    New Member
    • Oct 2006
    • 26

    #2
    hi,

    i tried working on your code. i did it the following way.

    Code:
    <html>
        <head>
        </head>
        <body>
            <input type="text" id="Text1" onFocus="this.focus();" onBlur="this.value = this.value + '.00' " value="">
        </body>
    </html>
    hope this helps.

    Comment

    • java
      New Member
      • Oct 2006
      • 10

      #3
      thanks a lot.

      Comment

      • java
        New Member
        • Oct 2006
        • 10

        #4
        if the situation is:
        User key in the number with decimal? how to verify that?

        what i mean is if user key in 10.00, it will b remain as 10.00.
        If user key in 10, it will appear as 10.00.


        thx..

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by java
          if the situation is:
          User key in the number with decimal? how to verify that?

          what i mean is if user key in 10.00, it will b remain as 10.00.
          If user key in 10, it will appear as 10.00.


          thx..
          You could search for the presence of a dot (using a regex) first before you do the change.

          Comment

          • AricC
            Recognized Expert Top Contributor
            • Oct 2006
            • 1885

            #6
            What is the number range that will be entered? You could combine a switch/if with one of these functions.

            Number.toFixed( #)
            or
            Number.toPrecis ion(#)

            HTH,
            Aric

            Comment

            • java
              New Member
              • Oct 2006
              • 10

              #7
              no range. any number can be used.

              any suggestion on the way to solve it? Any code?


              thx

              Comment

              • Thevercad
                New Member
                • Oct 2006
                • 26

                #8
                hi,

                use the following code. This might solve your problem:

                Code:
                <input type="text" onFocus="this.focus();" onBlur="checkDecimal(this,this.value)" value="" />
                the javascript function checkDecimal is below:

                Code:
                function checkDecimal(obj, objStr){
                    var objNumber;
                    if(isNaN(objStr) && objStr!=''){
                        alert('Value entered is not numeric');
                        objNumber = '0.00';
                    }
                    else if(objStr==''){
                        objNumber = '0.00';
                    }
                    else if(objStr.indexOf('.')!=-1){
                        if(((objStr.length) - (objStr.indexOf('.')))>3){
                            objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
                        }
                        if(objStr.indexOf('.')==0){
                            objStr = '0' + objStr;
                        }
                        var sLen = objStr.length;
                        var TChar = objStr.substr(sLen-3,3);
                        if(TChar.indexOf('.')==0){
                            objNumber = objStr;
                        }
                        else if(TChar.indexOf('.')==1){
                            objNumber = objStr + '0';
                        }
                        else if(TChar.indexOf('.')==2){
                            objNumber = objStr + '00';
                        }
                    }
                    else{
                        objNumber = objStr + '.00';
                    }
                    obj.value = objNumber;
                }
                This function will check, if the number entered is numeric, if the decimal numbers take more than two digits, if the first character is a dot, if it has just a dot at the end, or just one decimal number or both the decimal numbers or no decimal number at all.

                i hope there are no errors in it.. please check the opening and closing braces, as well as the variable names.

                Comment

                Working...