Code: Select all
$("#txt-Total").on("input", function() {
let value = $(this).val();
let regex = /^-?\d*\.?\d{0,2}$/;
if (regex.test(value)) {
// Valid input: allows negative sign, digits, and up to two decimal places
$(this).data('previousValue', value); // Store valid value
} else {
// Invalid input: restore to the last valid value
$(this).val($(this).data('previousValue') || '');
}
});< /code>