JSFiddle - React, Tailwind, and code Playground
by vlit
HTML
<label>Минимальная цена</label>
<input type="text" name="price" class="numm" maxlength="15">
JavaScript
(function($) {
$.fn.inputFilter = function(callback, errMsg) {
return this.on("input keydown keyup mousedown mouseup select contextmenu drop focusout", function(e) {
if (callback(this.value)) {
// Accepted value
if (["keydown", "mousedown", "focusout"].indexOf(e.type) >= 0) {
$(this).removeClass("input-error");
this.setCustomValidity("");
}
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
// Rejected value - restore the previous one
$(this).addClass("input-error");
this.setCustomValidity(errMsg);
this.reportValidity();
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
} else {
// Rejected value - nothing to restore
this.value = "";
}
});
};
}(jQuery));
$(".numm").inputFilter(function(value) {
return /^[0-9.]*$/i.test(value)
}, "Вводить можно только цифры");
$(".teleg").inputFilter(function(value) {
return /^[a-z0-9_]*$/i.test(value); //после 9 идет символ который разрешаем
}, "Вводить можно латинские буквы, цифры и нижнее подчеркивание.");
$('.numm').keyup((event) => {
let value = event.currentTarget.value.replace(/./g, '')
event.currentTarget.value = value.replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1.')
})