JSFiddle - React, Tailwind, and code Playground
by Marco
HTML
<input id="a" name="" type="text" value="$500.45">
JavaScript
(function ($, undefined) {
$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if('selectionStart' in el) {
pos = el.selectionStart;
} else if('selection' in document) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}
})(jQuery);
$('#a').on('keydown',function(e){
var keycode= (e.keyCode ? e.keyCode : e.which);
if(keycode == 8){
var position = $(this).getCursorPosition();
var ca=$(this).val().slice(0,position).split('')
var x=ca[ca.length-1]
if(x==='$'||x==='.'){e.preventDefault()}
}
if(keycode == 46){
var position = $(this).getCursorPosition();
var ca=$(this).val().slice(0,position+1).split('')
var x=ca[ca.length-1]
if(x==='$'||x==='.'){e.preventDefault()}
}
})