Download
Demo
- Overview
- Documents
User Rating: 4.6/5 ( 3 votes)
Your Rating:
Features
- Input filtering and formatting of HTML5 type="number" inputs.
- Helps ensure numeric keyboard appears on mobiles for entry.
- Assists in ensuring input box stays in viewport during entry.
- Does not rely on spinners, which can optionally be hidden.
- Mobile-first design also works on desktops, including IE6+.
- Extremely light weight at 2.62KB of code gzipped.
- Provides both formatted value and numeric value.
- Provides optional event hooks for your own code.
- As-you-type real-time validation doesn’t rely on browser.
- Custom formatting of displayed numbers.
- Localizable: currency symbol, location, etc.
- Fully compatible with PhoneGap and Apache Cordova.
- Fully compatible with jQuery Mobile & other frameworks.
- Requires either jQuery 1.7+ or Zepto.js v1.0+.
- Free license for both commercial and non-commercial use.
Source: numbox.org
1. INCLUDE JS AND CSS FILES
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script> <script src="jquery.numbox-1.1.0.min.js" type="text/javascript"></script> <link href="/incjquery.numbox-1.1.0.css" rel="stylesheet" type="text/css" />
2. HTML
<input id="myInput" type="number" value="" placeholder="Please enter a dollar amount" />
3. JAVASCRIPT
$(document).ready(function() { $('#myInput').NumBox();// defaults to dollars });
4. OPTIONS
Optional Property | Default Value | Usage & Permissible Values | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
type | 'currency' |
Handy presets that conveniently save you from having to set multiple properties. Makes your code more readable from a maintenance perspective. Since 'currency' is the default, calling .NumBox() without any parameters is the same as specifying a dollar currency format. If using a credit card type, please note that credit card checksums (Luhn algorithms) are not applied. NumBox allows incomplete numbers to be entered as this is a better user experience than wiping out incomplete entries.
|
||||||||||||||||||||||
places | 2 (integer) |
Specifies how many decimal places. Set to zero if you only want to work with integers. Not applicable for card types, EINs or SSNs. |
||||||||||||||||||||||
min | 0 (decimal) |
Minimum value permitted. Currently can only be zero. NumBox will be improved to permit negative numbers in future. Not applicable for card types, EINs or SSNs. |
||||||||||||||||||||||
max | 999999.99 (decimal) |
Maximum value permitted. Can be adjusted as desired. Must be the same or higher than the minimum value permitted. Not applicable for card types, EINs or SSNs. |
||||||||||||||||||||||
step | 'min' |
This is the HTML5 step attribute, which is the amount that the number increments or decrements when you press the up or down spinner buttons, which some browsers provide for type=number inputs. According to the W3C specifications, the step setting should also limit the number itself, but how this is implemented in browsers varies and should not be relied upon. If you plan to hide the spinner buttons, (recommended as a better user experience,) then you can just leave this set to 'min'. Please never set this to 'any' (an HTML5 option) since that stops some mobile devices from displaying a numeric keyboard.
|
||||||||||||||||||||||
symbol | '$' | Specifies a symbol to display alongside the number, either immediately to the left or right of the number. Can be set to empty quotes, '', if not desired. Can also contain multiple characters such as 'USD' or even words, as shown in the atomic radius example. Not applicable for card types, EINs or SSNs. | ||||||||||||||||||||||
location | 'l' |
Location of symbol in relation to the number being displayed. Not applicable for card types, EINs or SSNs.
|
||||||||||||||||||||||
separator | ',' or '-' | To make a long number more readable, you may want to segment it into groups of a fixed number of digits with a character separating each segment. This property specifies the character to be used to separate each segment, and, for real numbers, defaults to a comma. For credit cards, EINs and SSNs, the character defaults to a hyphen. You can specify your own character choice, but it cannot be a decimal point or number. Applicable for all types, including credit cards, EINs, SSNs and real numbers. Use empty quotes, '', if you don’t want to see a separating character. | ||||||||||||||||||||||
grouping | 3 or 4 (integer) | To make a long number more readable, you may want to segment it into groups of a fixed number of digits with a character separating each segment. This property specifies how many digits are to be grouped together into a segment. In the case of real numbers, segmenting occurs every x digits to the left of the decimal point. In all other cases (credit cards, EIN and SSN) segmenting occurs every x digits moving from left to right. Only applicable for ccard, ccard-16 and real number types. Not applicable for ccard-15, EIN or SSN types, which have irregular grouping intervals that NumBox handles directly. Must be an integer that is larger than zero. | ||||||||||||||||||||||
autoScroll | 'auto' |
When you tap on an HTML input box using a phone, typically your browser viewport is resized to the top half of your phone's screen, and the lower half is replaced with a virtual keyboard, so you can type an entry into the input box. However, sometimes the box you just tapped on is hidden by the virtual keyboard that just popped up. NumBox can optionally try to scroll the browser viewport so that the input box is scrolled up automatically to remains in view after the keyboard has appeared.
|
||||||||||||||||||||||
onFocus | '' (empty string) | You should set this property if you want to hook the focus event of the input. The value should be set to a string which is the name of the function you want to be called during the event. Please see the Hooking Event Handlers section for full details. | ||||||||||||||||||||||
onBlur | '' (empty string) | You should set this property if you want to hook the blur event of the input. The value should be set to a string which is the name of the function you want to be called during the event. Please see the Hooking Event Handlers section for full details. | ||||||||||||||||||||||
onKeydown | '' (empty string) | You should set this property if you want to hook the focus event of the input. The value should be set to a string which is the name of the function you want to be called during the event. Please see the Hooking Event Handlers section for full details. | ||||||||||||||||||||||
onInput | '' (empty string) | You should set this property if you want to hook the input or propertychange events of the input. The value should be set to a string which is the name of the function you want to be called during the event. Please see Hooking Event Handlers . | ||||||||||||||||||||||
onPaste | '' (empty string) | You should set this property if you want to hook the focus event of the input. The value should be set to a string which is the name of the function you want to be called during the event. Please see the Hooking Event Handlers section for full details. |
5. METHODS
Method (Click for Details) | Chainable | Usage Summary |
---|---|---|
.NumBox() .NumBox({ property: value, … }) .NumBox('setup') .NumBox('setup', { property: value, … }) |
Different ways of specifying the initial set up of NumBox against type="text" or type="number" inputs, with or without an optional list of configuration properties. Required first. Call once per input unless you use 'destroy' between calls. |
|
.NumBox('destroy') | Disassociates NumBox (including events) from inputs. | |
.NumBox('getFormatted') | Returns display formatted number as a string. e.g. $1,234.50. | |
.NumBox('getRaw') | Returns raw number for calculations, et cetera. e.g. 1234.5. | |
.NumBox('setRaw', rawValue) |
Lets you change NumBox value programmatically. Optional as NumBox picks up existing value during set up. |
|
.NumBox('autoScroll', 'auto' | true | false) |
Whether or not to help scroll input box higher up the screen. Handy on mobile devices where pop-up keyboards can appear. Also available as a property in 'setup'. Defaults to 'auto'. |
|
.NumBox('onFocus', 'fnCallbackName') |
Optional. Use if you need to hook gained focus events. Also available in 'setup'. Fn name is a quoted string. Callback fires on the input’s focus events. |
|
.NumBox('onBlur', 'fnCallbackName') |
Optional. Use if you need to hook lost focus events. Also available in 'setup'. Fn name is a quoted string. Callback fires on the input’s blur events. |
|
.NumBox('onKeydown', 'fnCallbackName') |
Optional. Use if you need to hook key press events. Also available in 'setup'. Fn name is a quoted string. Callback fires on the input’s keydown events. |
|
.NumBox('onInput', 'fnCallbackName') |
Optional. Use if you need to hook value is changing events. Also available in 'setup'. Fn name is a quoted string. Callback fires on the input & propertychange events. |
|
.NumBox('onPaste', 'fnCallbackName') |
Optional. Use if you need to hook paste events. Also available in 'setup'. Fn name is a quoted string. Callback fires on the input’s paste events (if supported.) |
6. CALLBACKS
Callback Method | Parameters Forwarded † | Important Notes on Use |
---|---|---|
onFocus | jQuery event object | Callback occurs when the input gains focus, during the focus event. |
onBlur | jQuery event object | Callback occurs when the input loses focus, during the blur event. |
onKeydown | jQuery event object | Callback usually occurs when a key is pressed, during the keydown event. You can use this event to obtain a key code, to identify which key was pressed. Some browsers do not send every key press, so use with caution. Google Chrome for Android, (not the native browser,) also has a major flaw, and sends a zero for all key presses, making it impossible to identify the key. |
onInput |
jQuery event object, input succeeded Boolean |
Callback usually occurs when the value of the input is changed by the user typing, while the input still has the focus. This is during the input event on all modern browsers. On older Microsoft browsers (IE8-) which don’t support the standard input event, NumBox hooks Microsoft’s proprietary propertychange event instead. However, since propertychange, unlike input, also fires when a value is changed via JavaScript, NumBox takes care not to hand off to your callback unless a keydown event is also fired. To add fuel to the fire, IE8 has a bug where it doesn't fire propertychange on the first key press. |
onPaste | jQuery event object | Callback sometimes occurs during a paste event, when the user is copy-pasting. This event should not be relied upon as it is not standard and only some browsers support it. Additionally, as NumBox blocks certain key presses during the keydown event, such as ctrl-v (Windows paste shortcut) then we may never get to this event. |
7. EXAMPLES
$('#money' ).NumBox(); // defaults to dollars $('#money' ).NumBox({symbol: '€', location: 'r'}); $('#plastic').NumBox({type: 'ccard'}); $('#percent').NumBox({type: 'percent'}); $('.NumBox' ).NumBox('destroy'); // '.NumBox' = all var forShow = $('#input').NumBox('getFormatted'); var forCalc = $('#input').NumBox('getRaw'); $('#input' ).NumBox('setRaw', 1234.5 ); $('#input' ).NumBox('setRaw', '1234.5'); $('.NumBox' ).NumBox('autoScroll', true); $('.NumBox' ).NumBox('onFocus', 'callback'); $('.NumBox' ).NumBox('onInput', 'callback'); function callback(event, valid) { // all callbacks pass the jQuery event object console.log ( event.currentTarget.id + ' : ' + event.type ); // input also has a change permitted Boolean if (typeof valid === 'boolean') { console.log ( '>> valid = ' + valid.toString() ); } }