steveosoule
2/9/2017 - 7:18 PM

Numeric Input

Numeric Input

/*
	<input type="number" class="numericOnly">
*/
$('.numericOnly').on('input', function(){

	var selectionStart = this.selectionStart,
		selectionEnd = this.selectionEnd,
		dirtyValue = this.value,
		cleanValue = dirtyValue.replace(/[^\d]/g, ''),
		lengthDifference = dirtyValue.length - cleanValue.length;

	selectionStart -= lengthDifference;
	selectionEnd -= lengthDifference;

	// set the value to the clean string
	this.value = cleanValue;

	// restore cursor postion
	this.setSelectionRange(selectionStart, selectionEnd);
});