TextChanged Postback not firing when pasting input

1 week ago 4
ARTICLE AD BOX

I’m having an issue with this JavaScript: when I paste a number, the TextChanged postback is not triggered, whereas it works correctly when I type the value manually.
It’s a general control, and the issue might be related to a postback

js

function verificaIncolla(event, sender) { try { var clipboardData = (event.clipboardData || window.clipboardData); var pasted = clipboardData ? clipboardData.getData('text') : null; if (!pasted) { return; } var containsLetter = /[A-Za-z\u00C0-\u024F]/.test(pasted); if (containsLetter) { return; } var cleaned = pasted.replace(/\s+/g, ''); cleaned = cleaned.replace(/[^0-9\.,\-]/g, ''); cleaned = cleaned.replace(/-/g, function (m, offset) { return (offset === 0) ? '-' : ''; }); if (cleaned.indexOf('-') > 0) { cleaned = cleaned.replace(/-/g, ''); } var separators = cleaned.match(/[.,]/g); if (separators && separators.length > 1) { var lastSep = cleaned.lastIndexOf('.') > cleaned.lastIndexOf(',') ? '.' : ','; cleaned = cleaned.replace(/[.,]/g, ''); if (cleaned.length > 2) { var before = cleaned.substring(0, cleaned.length - 2); var after = cleaned.substring(cleaned.length - 2); cleaned = before + lastSep + after; } else { cleaned = cleaned + lastSep; } } var start = sender.selectionStart; var end = sender.selectionEnd; var value = sender.value || ""; var newValue = value.substring(0, start) + cleaned + value.substring(end); sender.value = newValue; var newCaret = start + cleaned.length; if (typeof sender.setSelectionRange === 'function') { sender.setSelectionRange(newCaret, newCaret); } replaceAllDot(sender); VerificaTestoImporto(sender); if (typeof formattaNumero === 'function') { try { formattaNumero(sender,2); } catch (e) { } } } catch (e) { console.error('verificaIncolla error:', e); } } <asp:TextBox CssClass="form-control" OnTextChanged="txtDecimale_TextChanged" runat="server" ID="txtDecimale" onkeyup="VerificaTestoImporto(this)" onfocus="replaceAllDot(this);" onpaste="verificaIncolla(event,this)" ></asp:TextBox>

If I remove the onpaste event from the textbox, the TextChanged postback is triggered correctly.

Read Entire Article