猿问

无法复制和粘贴到 asp 文本框 asp.net C#

我的系统中有一个手机号码,我有一个 asp 文本框,这里的问题是无法将手机号码复制并粘贴到此 asp 文本框。我有一个只接受数字的 Onkeydown 函数。


我需要将否复制并粘贴到此文本框。


代码

<asp:TextBox ID="txtMobileNo" runat="server" autocomplete="off" 

    AutoCompleteType="Disabled" MaxLength="10" 

    onkeydown="return (event.keyCode >= 48 && event.keyCode <= 57) || 

        (event.keyCode >= 96 && event.keyCode <= 105) || event.which == 8 || 

        event.which == 46;"

    Style="height: 25px;" />


红糖糍粑
浏览 150回答 3
3回答

幕布斯7119047

我认为问题在于 JS 只是返回 true 或 false。我只是尝试粘贴使用Ctrl+V但没有任何反应,大概是因为 JS 正在读取我的键击以进行粘贴,但它们不是数字。但是,如果我右键单击鼠标并选择粘贴,它就会起作用。您可能必须摆脱 JS,或添加更多 keyCodes 以接受Ctrl+V.

慕娘9325324

要允许用户使用 Ctrl+V 或 Cmd+V 粘贴,检查keyCode您的onkeydown事件中的那些,然后检查粘贴的文本是否是事件中的数字onpaste,这是一个完整的示例:<asp:TextBox ID="txtMobileNo" runat="server"&nbsp;autocomplete="off" AutoCompleteType="Disabled" MaxLength="10"onkeydown="return validateInput(event);"onpaste="validateClipboard(event)"Style="height: 25px;" /><script type="text/javascript">&nbsp; &nbsp; function validateInput(event) {&nbsp; &nbsp; &nbsp; &nbsp; // Check if Ctrl+V or Cmd+V was pressed&nbsp; &nbsp; &nbsp; &nbsp; if ((event.ctrlKey || event.metaKey) && event.keyCode == 86) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; // Check if numbers input&nbsp; &nbsp; &nbsp; &nbsp; } else if ((event.keyCode >= 48 && event.keyCode <= 57) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (event.keyCode >= 96 && event.keyCode <= 105) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.which == 8 || event.which == 46) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // All checks failed&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; function validateClipboard(event) {&nbsp; &nbsp; &nbsp; &nbsp; // Prevent clipboard data pasting&nbsp; &nbsp; &nbsp; &nbsp; event.stopPropagation();&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; // Get clipboard text data&nbsp; &nbsp; &nbsp; &nbsp; var clipboardData = event.clipboardData || window.clipboardData;&nbsp; &nbsp; &nbsp; &nbsp; var pastedData = clipboardData.getData('Text');&nbsp; &nbsp; &nbsp; &nbsp; // Check if pasted data is a number, if true paste to text box&nbsp; &nbsp; &nbsp; &nbsp; if (isNaN(pastedData) === false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var txtBox = document.getElementById('MainContent_txtMobileNo');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; txtBox.value += pastedData;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script>

慕田峪9158850

我不确定您要匹配哪种数字格式,我假设您有这种格式的数字9999999999(十位数字)。这是我能想到的最佳解决方案:<asp:TextBox ID="txtMobileNo" runat="server" autocomplete="off"&nbsp; &nbsp; &nbsp; &nbsp; AutoCompleteType="Disabled" MaxLength="10"&nbsp; &nbsp; &nbsp; &nbsp; onblur="return validateNumber(this)"&nbsp; &nbsp; &nbsp; &nbsp; Style="height: 25px;" />keydown请注意,我使用了事件而不是onblur事件,这是因为在他们完成文本框输入后和离开文本框时检查数字是一种很好的做法。<script>&nbsp; &nbsp; function validateNumber(evt) { // this will get executed when user has stopped typing or pasting the value.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var isValid = isValidMobileNumber(evt.value);&nbsp; &nbsp; &nbsp; &nbsp; if (!isValid)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evt.value = ''; // Clear if the number is not valid.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return isValid;&nbsp; &nbsp; }&nbsp; &nbsp; function isValidMobileNumber(number) {&nbsp; &nbsp; &nbsp; &nbsp; var phoneRegex = /^\d{10}$/; /* to verify 10 digits in number */&nbsp; &nbsp; &nbsp; &nbsp; /* var phoneRegex = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/; // to verify 10 digits number with (-) in it. 999-999-9999 */&nbsp; &nbsp; &nbsp; &nbsp; var digits = number.replace(/\D/g, "");&nbsp; &nbsp; &nbsp; &nbsp; return phoneRegex.test(digits);&nbsp; &nbsp; }&nbsp; &nbsp; // The code below can be removed, read the explanation below the code.&nbsp; &nbsp; document.getElementById('<%= txtMobileNo.ClientID %>').addEventListener('paste', (e) => {&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; const text = (e.originalEvent || e).clipboardData.getData('text/plain');&nbsp; &nbsp; &nbsp; &nbsp; if (isValidMobileNumber(text))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.document.execCommand('insertText', false, text);&nbsp; &nbsp; });</script>请注意,我已经paste为您的文本框绑定了一个事件,每次有人在文本框中粘贴内容后都会立即触发该事件。但是,从技术上讲,您可以从此处删除此事件,程序将正常运行,这是因为onblur在您粘贴数据后无论如何都会触发事件,最终将验证粘贴的数据。因此,由您决定,您是希望在粘贴时立即验证数据,还是希望在用户离开控件时(粘贴或键入任何内容后)验证数据。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答