Làm cách nào để hạn chế người dùng nhập các ký tự đặc biệt trong TextBox bằng JavaScript?

Theo mô tả và mã của bạn, bạn có thể cho chúng tôi biết id điều khiển bị lỗi ở đâu không? Tôi thấy vấn đề của bạn là thiếu điều khiển để hiển thị lỗi

Thêm chi tiết, bạn có thể tham khảo các mã dưới đây

 <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(35); //#
        specialKeys.push(37); //%
        specialKeys.push(43); //+
        specialKeys.push(45); //-
        specialKeys.push(61); //=
        specialKeys.push(64); //@

        function IsAlphaNumeric(e) {
            var ret;
            var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
            ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
            if (specialKeys.indexOf(keyCode) != -1) {
                ret = true;
            }
            document.getElementById("error").style.display = ret ? "none" : "inline";
              return ret;
        
        }
    </script>  


 <table align="center" class="auto-style10">
                <tr>
                    <td class="auto-style1" colspan="5">
                        <asp:ScriptManager ID="ScriptManager1" runat="server">
                        </asp:ScriptManager>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1" colspan="5">
<%--                        <asp:Image ID="Image1" runat="server" Height="88px" ImageUrl="~/img/serv.png" Width="353px" />--%>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="auto-style11" colspan="3">
                        <asp:Label ID="Label11" runat="server" Text="Label" Width="270px"></asp:Label>
                        <asp:Label ID="Label10" runat="server" Text=":المستخدم"></asp:Label>
                    </td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="auto-style1">
                        <asp:Button ID="btnback" runat="server" CssClass="auto-style3" Height="35px" Text="رجوع للرئيسية" Width="140px" OnClick="btnback_Click" CausesValidation="False" />
                    </td>
                    <td class="auto-style1" colspan="2">
                        <asp:Button ID="Button1" runat="server" CssClass="auto-style3" Height="35px" Text="تسجيل خدمة جديده" Width="150px" OnClick="Button1_Click" CausesValidation="False" />
                    </td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td colspan="2">
                        <asp:HiddenField ID="hfdoc_no" runat="server" />
                    </td>
                    <td></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td colspan="2">
                        <asp:TextBox ID="txtdate" runat="server" Height="30px" Width="230px" Enabled="False" CssClass="auto-style4" BorderColor="#F0F0F0" BorderStyle="None"></asp:TextBox>
                    </td>
                    <td class="auto-style1">:التاريخ</td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td colspan="2">
                        <asp:DropDownList ID="DropDownList1" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style4" Height="35px" Width="240px" BackColor="White" Enabled="False">
                        </asp:DropDownList>
                    </td>
                    <td class="auto-style6"><strong>:المدينة</strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td colspan="2">
                        <asp:DropDownList ID="DropDownList2" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style4" Height="35px" Width="240px" BackColor="White" Enabled="False">
                        </asp:DropDownList>
                    </td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label3" runat="server" CssClass="auto-style4" Text=":الشركة"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td colspan="2">
                        <asp:TextBox ID="TextBox1" runat="server" onpaste="return false" oncopy="return false" oncut="return false" Height="30px" Style="font-size: large; font-weight: 700" Width="230px" Enabled="False"></asp:TextBox>
                        <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" TargetControlID="TextBox1" ServiceMethod="GetCompletionList" Enabled="False" CompletionInterval="500"></cc1:AutoCompleteExtender>
                    </td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label4" runat="server" CssClass="auto-style4" Text=":الطبيب"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtser" CssClass="auto-style12" ErrorMessage="ادخل بيان الخدمة">*</asp:RequiredFieldValidator>
                    </td>
                    <td colspan="2">
                        <asp:TextBox onkeypress="return IsAlphaNumeric(event);" ID="txtser" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style4" Height="80px" TextMode="MultiLine" Width="238px" BackColor="White" autocomplete="off"></asp:TextBox>
                    </td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label5" runat="server" CssClass="auto-style4" Text=":الخدمة"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td> <asp:Label ID="error" runat="server" CssClass="labeldisplay" Text="Special characters" ForeColor="Red"></asp:Label></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtqun" CssClass="auto-style12" ErrorMessage="ادخل الكمية">*</asp:RequiredFieldValidator>
                    </td>
                    <td class="auto-style7"><strong>
                        <asp:TextBox ID="txtqun" onkeypress="CheckNumeric(event);" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style8" Width="79px" Enabled="False" autocomplete="off"></asp:TextBox>
                    </strong></td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label6" runat="server" CssClass="auto-style4" Text=":الكمية"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtprice" CssClass="auto-style12" ErrorMessage="ادخل السعر">*</asp:RequiredFieldValidator>
                    </td>
                    <td class="auto-style5"><strong>
                        <asp:TextBox ID="txtprice" onkeypress="CheckNumeric(event);" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style8" Width="79px" Enabled="False" autocomplete="off"></asp:TextBox>
                    </strong></td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label7" runat="server" CssClass="auto-style4" Text=":السعر"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="auto-style5"><strong>
                        <asp:TextBox ID="txttotal" runat="server" onpaste="return false" oncopy="return false" oncut="return false" CssClass="auto-style8" Width="79px" Enabled="False" autocomplete="off"></asp:TextBox>
                    </strong></td>
                    <td class="auto-style1"><strong>
                        <asp:Label ID="Label8" runat="server" CssClass="auto-style4" Text=":الاجمالي"></asp:Label>
                    </strong></td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="auto-style5">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><strong>
                        <asp:Button ID="btnsave" runat="server" CssClass="auto-style4" OnClick="btnsave_Click" Text="حفظ" Width="88px" Enabled="False" />
                    </strong></td>
                    <td class="auto-style5">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td class="auto-style5">&nbsp;</td>
                    <td>
                        <asp:Label ID="lblman_no" runat="server"></asp:Label>
                    </td>
                    <td class="auto-style9">&nbsp;</td>
                </tr>
            </table>

Kết quả

Làm cách nào để hạn chế người dùng nhập các ký tự đặc biệt trong TextBox bằng JavaScript?

Trân trọng,

Yijing Sun

Người dùng1535942433 đã đăng

Xin chào alhakimy,

Theo mô tả của bạn, tôi không hiểu rõ yêu cầu của bạn. Tôi đoán rằng bạn cần ngăn người dùng nhập các ký tự đặc biệt và không hiển thị thông báo lỗi. Bạn có cần rằng khi người dùng nhập các ký tự đặc biệt, các ký tự không thể hiển thị?

Nếu bạn cần yêu cầu, theo như tôi nghĩ, phương pháp này có thể khiến người dùng không biết các ký tự đặc biệt không thể nhập được. Tôi đề nghị bạn có thể cung cấp cho người dùng thông báo rằng không thể nhập các ký tự đặc biệt

Các mã bên dưới có tác dụng ngăn người dùng nhập các ký tự đặc biệt và không thể nhập các ký tự đặc biệt

Làm cách nào để hạn chế người dùng chỉ nhập các ký tự trong hộp văn bản bằng JavaScript?