Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <label>
	Numbers only:
	<input class="num" type="text" />
	<input class="num" type="text" />
</label>
<a href="#" id="remove">Remove numeric</a>
              
            
!

CSS

              
                label
	display: block
              
            
!

JS

              
                class Numeric {
	numeric(element, config, callback) {
		if (typeof config === "boolean") {
			config = { decimal: config };
		}
		config = config || {};
		// if config.negative undefined, set to true (default is to allow negative numbers)
		if (typeof config.negative === "undefined") {
			config.negative = true;
		}
		// set decimal point
		let decimal = config.decimal === false ? "" : config.decimal || ".";
		// allow negatives
		const negative = config.negative === true;
		// callback function
		callback = typeof callback === "function" ? callback : function () {};
		// scale
		let scale;
		if (typeof config.scale === "number") {
			if (config.scale == 0) {
				decimal = false;
				scale = -1;
			} else scale = config.scale;
		} else scale = -1;
		// precision
		let precision;
		if (typeof config.precision === "number") {
			precision = config.precision;
		} else precision = 0;

		element["numeric.decimal"] = decimal;
		element["numeric.negative"] = negative;
		element["numeric.callback"] = callback;
		element["numeric.scale"] = scale;
		element["numeric.precision"] = precision;
		element.onkeypress = numericKeypress;
		element.onkeyup = numericKeyup;
		element.onBlur = numericBlur;

		function getSelectionStart(o) {
			if (o.createTextRange) {
				const r = document.selection.createRange().duplicate();
				r.moveEnd("character", o.value.length);
				if (r.text === "") {
					return o.value.length;
				}
				return o.value.lastIndexOf(r.text);
			}
			return o.selectionStart;
		}

		function setSelection(o, p) {
			// if p is number, start and end are the same
			if (typeof p === "number") {
				p = [p, p];
			}
			// only set if p is an array of length 2
			if (p && p.constructor == Array && p.length == 2) {
				if (o.createTextRange) {
					const r = o.createTextRange();
					r.collapse(true);
					r.moveStart("character", p[0]);
					r.moveEnd("character", p[1]);
					r.select();
				} else if (o.setSelectionRange) {
					o.focus();
					o.setSelectionRange(p[0], p[1]);
				}
			}
		}

		function numericKeypress(e) {
			// get decimal character and determine if negatives are allowed
			const decimal = this["numeric.decimal"];
			const negative = this["numeric.negative"];
			// get the key that was pressed
			const key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if (key == 13 && this.nodeName.toLowerCase() == "input") {
				return true;
			} else if (key == 13) {
				return false;
			}
			let allow = false;
			// allow Ctrl+A
			if (
				(e.ctrlKey && key == 97) /* firefox */ ||
				(e.ctrlKey && key == 65) /* opera */
			) {
				return true;
			}
			// allow Ctrl+X (cut)
			if (
				(e.ctrlKey && key == 120) /* firefox */ ||
				(e.ctrlKey && key == 88) /* opera */
			) {
				return true;
			}
			// allow Ctrl+C (copy)
			if (
				(e.ctrlKey && key == 99) /* firefox */ ||
				(e.ctrlKey && key == 67) /* opera */
			) {
				return true;
			}
			// allow Ctrl+Z (undo)
			if (
				(e.ctrlKey && key == 122) /* firefox */ ||
				(e.ctrlKey && key == 90) /* opera */
			) {
				return true;
			}
			// allow or deny Ctrl+V (paste), Shift+Ins
			if (
				(e.ctrlKey && key == 118) /* firefox */ ||
				(e.ctrlKey && key == 86) /* opera */ ||
				(e.shiftKey && key == 45)
			) {
				return true;
			}
			// if a number was not pressed
			if (key < 48 || key > 57) {
				const value = this.value;
				/* '-' only allowed at start and if negative numbers allowed */
				if (
					value.indexOf("-") !== 0 &&
					negative &&
					key == 45 &&
					(value.length === 0 || parseInt(getSelectionStart(this), 10) === 0)
				) {
					return true;
				}
				/* only one decimal separator allowed */
				if (
					decimal &&
					key == decimal.charCodeAt(0) &&
					value.indexOf(decimal) != -1
				) {
					allow = false;
				}

				// check for other keys that have special purposes
				if (
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				) {
					allow = false;
				} else {
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if (typeof e.charCode !== "undefined") {
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if (e.keyCode == e.which && e.which !== 0) {
							allow = true;
							// . and delete share the same code, don't allow . (will be set to true later if it is the decimal point)
							if (e.which == 46) {
								allow = false;
							}
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if (e.keyCode !== 0 && e.charCode === 0 && e.which === 0) {
							allow = true;
						}
					}
				}
				// if key pressed is the decimal and it is not already in the field
				if (decimal && key == decimal.charCodeAt(0)) {
					if (value.indexOf(decimal) == -1) {
						allow = true;
					} else {
						allow = false;
					}
				}
			}
			// if a number key was pressed.
			else {
				// If scale >= 0, make sure there's only <scale> characters
				// after the decimal point.
				if (this["numeric.scale"] >= 0) {
					const decimalPosition = this.value.indexOf(decimal);
					// If there is a decimal.
					if (decimalPosition >= 0) {
						const decimalsQuantity = this.value.length - decimalPosition - 1;
						// If the cursor is after the decimal.
						if (getSelectionStart(this) > decimalPosition)
							allow = decimalsQuantity < this["numeric.scale"];
						else {
							const integersQuantity = this.value.length - 1 - decimalsQuantity;
							// If precision > 0, integers and decimals quantity should not be greater than precision
							if (integersQuantity < this["numeric.precision"] - this["numeric.scale"])
								allow = true;
							else allow = false;
						}
					}
					// If there is no decimal
					else {
						if (this["numeric.precision"] > 0)
							allow =
								this.value.replace(this["numeric.decimal"], "").length <
								this["numeric.precision"] - this["numeric.scale"];
						else allow = true;
					}
				}
				// If precision > 0, make sure there's not more digits than precision
				else if (this["numeric.precision"] > 0)
					allow =
						this.value.replace(this["numeric.decimal"], "").length <
						this["numeric.precision"];
				else allow = true;
			}
			return allow;
		}

		function numericKeyup(e) {
			let val = this.value;
			if (val && val.length > 0) {
				// get carat (cursor) position
				const carat = getSelectionStart(this);
				// get decimal character and determine if negatives are allowed
				const decimal = this["numeric.decimal"];
				const negative = this["numeric.negative"];

				// prepend a 0 if necessary
				if (decimal !== "" && decimal !== null) {
					// find decimal point
					const dot = val.indexOf(decimal);
					// if dot at start, add 0 before
					if (dot === 0) {
						this.value = "0" + val;
					}
					// if dot at position 1, check if there is a - symbol before it
					if (dot == 1 && val.charAt(0) == "-") {
						this.value = "-0" + val.substring(1);
					}
					val = this.value;
				}

				// if pasted in, only allow the following characters
				const validChars = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "-", decimal];
				// get length of the value (to loop through)
				const length = val.length;
				// loop backwards (to prevent going out of bounds)
				for (let i = length - 1; i >= 0; i--) {
					const ch = val.charAt(i);
					// remove '-' if it is in the wrong place
					if (i !== 0 && ch == "-") {
						val = val.substring(0, i) + val.substring(i + 1);
					}
					// remove character if it is at the start, a '-' and negatives aren't allowed
					else if (i === 0 && !negative && ch == "-") {
						val = val.substring(1);
					}
					let validChar = false;
					// loop through validChars
					for (let j = 0; j < validChars.length; j++) {
						// if it is valid, break out the loop
						if (ch == validChars[j]) {
							validChar = true;
							break;
						}
					}
					// if not a valid character, or a space, remove
					if (!validChar || ch == " ") {
						val = val.substring(0, i) + val.substring(i + 1);
					}
				}
				// remove extra decimal characters
				const firstDecimal = val.indexOf(decimal);
				if (firstDecimal > 0) {
					for (let k = length - 1; k > firstDecimal; k--) {
						const chch = val.charAt(k);
						// remove decimal character
						if (chch == decimal) {
							val = val.substring(0, k) + val.substring(k + 1);
						}
					}
					// remove numbers after the decimal so that scale matches.
					if (this["numeric.scale"] >= 0)
						val = val.substring(0, firstDecimal + this["numeric.scale"] + 1);
					// remove numbers so that precision matches.
					if (this["numeric.precision"] > 0)
						val = val.substring(0, this["numeric.precision"] + 1);
				}
				// limite the integers quantity, necessary when user delete decimal separator
				else if (this["numeric.precision"] > 0)
					val = val.substring(0, this["numeric.precision"] - this["numeric.scale"]);

				// set the value and prevent the cursor moving to the end
				this.value = val;
				setSelection(this, carat);
			}
		}

		function numericBlur() {
			const decimal = this["numeric.decimal"];
			const callback = this["numeric.callback"];
			const val = this.value;
			if (val !== "") {
				const re = new RegExp("^\\d+$|^\\d*" + decimal + "\\d+$");
				if (!re.exec(val)) {
					callback.apply(this);
				}
			}
		}
	}
	removeNumeric(elem) {
		elem["numeric.decimal"] = null;
		elem["numeric.negative"] = null;
		elem["numeric.callback"] = null;
		elem.onkeypress = null;
		elem.onblur = null;
		elem.onkeyup = null;
	}
}
const n = new Numeric();

const inp = document.querySelectorAll(".num");
[].forEach.call(inp, (el) => n.numeric(el, { precision: 2, decimal: false }));

document
	.querySelector("#remove")
	.addEventListener("click", () =>
		[].forEach.call(inp, (el) => n.removeNumeric(el))
	);

              
            
!
999px

Console