Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Trying to limit a input type=text only decimal values with 2 digits after decimal

Trying to limit a input type=text only decimal values with 2 digits after decimal

Scheduled Pinned Locked Moved JavaScript
javascripthelp
3 Posts 2 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    I am working on an Ember application, I have a following JavaScript Code written to limit the entry of digits after decimal, this function is called in Key-Down event. Its working but what it is doing it, it allowing 3 digits after decimal but when I am posting its value on the Service, that is taking correctly 2 digits after decimal but what it is showing in the Textbox is 3 digits after decimal. And another request if possible is, can I do the same without using id attribute on the Textbox. Any help please, thanks in advance.

    allowOnly2Decimals: function (e1, event) {
    var boxId = '#' + event.path[0].id;

                if ((event.keyCode == 190) || (event.keyCode == 110)) {
                    this.decimalPressed = true;
                }
    
                if (this.decimalPressed) {
                    var t = e1.toString().split('.');
    
                    if (t\[1\] != null) {
                        if (t\[1\].length == 2) {
                            this.limitDecimal = e1;                           
                            $(boxId).val(e1);
                        }
    
                        if (t\[1\].length >= 2) {
                            e1 = this.limitDecimal;
                            $(boxId).val(e1);
    
                            return false;
                        }
                    }
                }                          
            }
    
    Richard DeemingR 1 Reply Last reply
    0
    • S simpledeveloper

      I am working on an Ember application, I have a following JavaScript Code written to limit the entry of digits after decimal, this function is called in Key-Down event. Its working but what it is doing it, it allowing 3 digits after decimal but when I am posting its value on the Service, that is taking correctly 2 digits after decimal but what it is showing in the Textbox is 3 digits after decimal. And another request if possible is, can I do the same without using id attribute on the Textbox. Any help please, thanks in advance.

      allowOnly2Decimals: function (e1, event) {
      var boxId = '#' + event.path[0].id;

                  if ((event.keyCode == 190) || (event.keyCode == 110)) {
                      this.decimalPressed = true;
                  }
      
                  if (this.decimalPressed) {
                      var t = e1.toString().split('.');
      
                      if (t\[1\] != null) {
                          if (t\[1\].length == 2) {
                              this.limitDecimal = e1;                           
                              $(boxId).val(e1);
                          }
      
                          if (t\[1\].length >= 2) {
                              e1 = this.limitDecimal;
                              $(boxId).val(e1);
      
                              return false;
                          }
                      }
                  }                          
              }
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      No need to use Javascript:

      <input type="number" scale="0.01" />

      Or:

      <input type="text" pattern="^\d+(\.\d{0,2})?$" />


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        No need to use Javascript:

        <input type="number" scale="0.01" />

        Or:

        <input type="text" pattern="^\d+(\.\d{0,2})?$" />


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        S Offline
        S Offline
        simpledeveloper
        wrote on last edited by
        #3

        But it didn't work Here is what I have put

        ${{input type="number" id=p.ViolationTypeId value=p.PenaltyAssessed maxlength="5" scale="0.01" pattern="^\d+(\.\d{0,2})?$" focusOut=(action 'saveTotalPenalty' p model.id p.PenaltyAssessed)}}

        I tried both individually and by putting both, still it didn't work, is Ember different, I don't know.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups