GithubHelp home page GithubHelp logo

malcolmkiano / decimal-input Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 12 KB

A JavaScript plugin that forces decimal entry.

Home Page: https://malcolmkiano.github.io/decimal-input/demo/

JavaScript 100.00%
javascript plugin decimal input-mask

decimal-input's Introduction

Decimal Input

A JavaScript plugin that forces decimal entry on input elements. I made this because I'm working on a web-app that requires decimal input, however, the default iOS numeric keypad does not include a period. The height feature was added just because I needed it for my app, and thought it might be useful.
See a demo.

Installation

All you have to do is download the decimal-input.js file and include it in your project. That's it.

Usage

Include the decimal-input.js file at the bottom of your body and initialize it. By default, the selector to activate your inputs is .decimal, but you can change this to anything else in your initialization.

<body>
    <!-- rest of your code -->
    <input class="decimal" type="number"/>
    ...

    <script type="text/javascript" src="link/to/decimal-input.js"></script>
    <script type="text/javascript">
      decimalInput();
    </script>

</body>

Configuration

By default, this sets your inputs to 2 decimal places, however there are a few global configuration options, as well as input-level options.

Global options

  • selector: this sets the selector that decimal-input.js will watch and automatically format. Default value is .decimal. You should primarily use class selectors for targets if using multiple inputs.
decimalInput({
    selector: ".decimal"
})
  • format: this sets the formatting type for the inputs. Can be standard or height. Default value is standard.
decimalInput({
    format: "standard"
})
  • length: this sets the number of decimal places used in formatting your inputs. Default value is 2. This value only affects inputs displaying in the standard format.
decimalInput({
    length: 2
})
  • validation: this sets the type of validation to be used on height inputs. Can be soft or hard. Default value is soft.
    • Soft validation marks the input as invalid and you can hook onto this with css to show it as such.
    • Hard validation calculates the actual value in feet and inches and replaces the entry in the input.
decimalInput({
    validation: "soft"
})

Input-level options

All the input level options are applied using the data attribute. These override the global options only for the input they are applied on.

  • format: this, just like the global option, sets the formatting type for the specific input it's used on. Can be standard or height.
<input class="decimal" type="number" data-format="standard"/>
  • length: this sets the number of decimal places to be displayed for the specific input, but only affects the standard format.
<input class="decimal" type="number" data-length="2"/>

decimal-input's People

Contributors

malcolmkiano avatar

Stargazers

 avatar

Watchers

 avatar

decimal-input's Issues

Browsers on Android don't return keyCodes properly on the keydown event.

Browsers on Android return the keyCode 229 for pretty much everything and this creates an issue when the user tries to delete their entry. This also allows external input of the period "." which should not work under normal conditions.

I'm using an emulator, so that might be contributing to this issue, but I'm unsure.

If the input has a value, it doesn't convert well

Hello!

I have an input with the value 30.
When I run decimalInput(); it converts the value to 300.00 instead of 30.00.

It looks like it doesn't support pre-existing values at all.

Thank you so much, great library.
Best,
M

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.