且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

文本框只允许字母.......在C#文本中显示代码只允许在C#中使用相同的字母数字

更新时间:2021-07-23 23:02:37

谷歌是你的朋友。例如,参见 c# - 如何制作一个文本框只接受号码? - 堆栈溢出 [ ^ ]。虽然不能完全解决您的问题,但第一个答案说明了该技术。
Google is your friend. See for instance c# - How do I make a textbox that only accepts numbers? - Stack Overflow[^]. While not exactly solving your problem, the first answer illustrates the technique.


对于ASP.NET,您不希望在C#中执行此操作 - 您希望在JavaScript中执行此操作,以便用户无法输入不良信息。在C#中执行此操作意味着在用户完成整个表单或每个字符时往返服务器。前者对于用户来说是一种痛苦,因为他们最后被告知这是错误的并且必须回去修复它,后者是缓慢而笨拙的。使用JS意味着它在客户端完成,服务器不参与。

Javascript验证:使用JavaScript验证TextBox [ ^ ]
For ASP.NET you don't want to do it in C# - you want to do it in JavaScript so that the user can't enter "bad" info. Doing it in C# means a round trip to the server either when the user is finished with the whole form, or for each character. The former is a pain for the user as they get told at the end "that was wrong" and have to go back to fix it, the latter is slow and awkward. Using JS means it's done at the client end and the server isn't involved.
Javascript validation: Validate TextBox using JavaScript[^]