如何在 ASP.NET 文本框中强制输入大写?
我正在编写一个 ASP.NET 应用程序。 我的网络表单上有一个文本框,我想强制用户输入的所有内容均为大写。 我想在前端做这个。 您还应该注意,此文本框上有一个验证控件,因此我想确保该解决方案不会干扰 ASP.NET 验证。
澄清: CSS 文本转换似乎使用户输入以大写形式显示。 然而,在幕后,由于验证控制失败,它仍然是小写。 您会看到,我的验证控件检查是否输入了有效的状态代码,但是我使用的正则表达式仅适用于大写字符。
I'm writing an ASP.NET application. I have a textbox on a webform, and I want to force whatever the user types to upper case. I'd like to do this on the front end. You should also note that there is a validation control on this textbox, so I want to make sure the solution doesn't interfere with the ASP.NET validation.
Clarification:
It appears that the CSS text transform makes the user input appear in uppercase. However, under the hood, it's still lower case as the validation control fails. You see, my validation control checks to see if a valid state code is entered, however the regular expression I'm using only works with uppercase characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
为什么不结合使用 CSS 和后端呢? Use:
在文本框中,以及在代码隐藏中 use:
您还可以轻松更改验证器上的正则表达式以使用小写和大写字母。 这可能是比强制使用大写更简单的解决方案。
Why not use a combination of the CSS and backend? Use:
on the TextBox, and in your codebehind use:
You can also easily change your regex on the validator to use lowercase and uppercase letters. That's probably the easier solution than forcing uppercase on them.
在文本框上使用 CSS 样式。 你的 CSS 应该是这样的:
Use a CSS style on the text box. Your CSS should be something like this:
好的,经过测试,这是一个更好、更干净的解决方案。
Okay, after testing, here is a better, cleaner solution.
您可以拦截按键事件,取消小写事件,并将其大写版本附加到输入中:
这适用于 Firefox 和 Internet Explorer。 您可以在此处查看其实际效果。
You can intercept the key press events, cancel the lowercase ones, and append their uppercase versions to the input:
This works in both Firefox and Internet Explorer. You can see it in action here.
我使用一个简单的内联语句
如果你不想使用CSS类,你可以只使用内联样式语句。(这个只是明显地变成大写):)
在服务器端使用
I use a simple one inline statement
If you don't want to use css classes you can just use inline style statement.(This one just visibly make uppercase) :)
On Server side use
文本转换
CSS 属性指定如何将元素的文本大写。 它可用于使文本以全大写或全小写形式显示
CssClass
WebControl.CssClass 属性,
您可以了解更多相关信息 - https://developer.mozilla.org/en/docs/Web/CSS/text-transform
https://msdn .microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.cssclass(v=vs.110).aspx
使用
Style="text -transform: uppercase;"
或CssClass="upper"
text-transform
CSS property specifies how to capitalize an element's text. It can be used to make text appear in all-uppercase or all-lowercase
CssClass
WebControl.CssClass Property
you can learn more about it - https://developer.mozilla.org/en/docs/Web/CSS/text-transform
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.cssclass(v=vs.110).aspx
use
Style="text-transform: uppercase;"
orCssClass="upper"
我今天刚刚做了类似的事情。 这是修改后的版本:
基本上,脚本附加了一个在文本框失去焦点时触发的事件。
I just did something similar today. Here is the modified version:
Basically, the script attaches an event that fires when the text box loses focus.
我会使用 jQuery 来完成此操作。
您必须在
/script
文件夹中包含 jQuery 库。I would do this using jQuery.
You must have the jQuery library in the
/script
folder.我对四个流行的浏览器版本对这个问题做了一些分析。
我找到的答案是在 keyup 上结合 style 标签来实现这一点。
I have done some analysis about this issue on four popular browser versions.
the answer I found is to implement this on keyup in conjunction with the style tag.
将文本框上的样式设置为 text-transform:大写?
Set the style on the textbox as text-transform: uppercase?
在前端使用文本转换 CSS,然后在验证之前在字符串服务器端使用 toUpper 方法。
Use the text-transform CSS for the front-end and then use the toUpper method on your string server-side before you validate.
CSS 在这里可能会有所帮助。
这有帮助吗?
CSS could be of help here.
does this help?
这是一个对我有用的解决方案。
http://plugins.jquery.com/project/bestupper
您必须从 < 获取 JavaScript a href="http://plugins.jquery.com/files/jquery.bestupper.min.js.txt" rel="nofollow">http://plugins.jquery.com/files/jquery.bestupper.min。 js.txt 就可以了。
奇迹般有效!
here is a solution that worked for me.
http://plugins.jquery.com/project/bestupper
You have to get the JavaScript from http://plugins.jquery.com/files/jquery.bestupper.min.js.txt and there you go.
Works like a charm!
我意识到有点晚了,但我找不到与 ASP.NET AJAX 一起使用的好答案,所以我修复了上面的代码:
像这样使用:
I realize it is a bit late, but I couldn't find a good answer that worked with ASP.NET AJAX, so I fixed the code above:
Used like so:
JavaScript 具有字符串的“toUpperCase()”函数。
所以,沿着这些思路:
JavaScript has the "toUpperCase()" function of a string.
So, something along these lines:
这是一种可重用的方法。 任何数量的文本框都可以通过这种方式完成,无需命名。
通过更改 docReady 中的选择器可以实现页面范围的解决方案。
我的示例使用失去焦点,问题在输入时没有指定。 如果这在您的场景中很重要,您可以触发更改。
This is a reusable approach. Any number of textboxes can be done this way w/o naming them.
A page wide solution could be achieved by changing the selector in docReady.
My example uses lost focus, the question did not specify as they type. You could trigger on change if thats important in your scenario.
最少 8 个字符 至少 1 个字母和 1 个数字
有效密码示例:pass1234 OR PaSs1234 OR PASS1234
至少 8 个字符 至少 1 个字母、1 个数字和 1 个特殊字符
有效密码示例:pass@123 或 PaSS#123 或 PASS@123
至少 8 个字符,至少 1 个大写字母、1 个小写字母和 1 个数字
有效密码示例:PaSs1234 或 pASS1234
至少 8 个字符,至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符 <
asp:TextBox ID="txtPolicy4" runat="server">
有效密码示例:PaSs@123 或 pAss@123
最少 8 个、最多 10 个字符 至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符
有效密码示例:PaSs@123
Minimum 8 characters at least 1 Alphabet and 1 Number
<asp:TextBox ID="txtPolicy1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex1" runat="server" ControlToValidate="txtPolicy1"
ValidationExpression="^(?=.[A-Za-z])(?=.\d)[A-Za-z\d]{8,}$" ErrorMessage="Password must contain: Minimum 8 characters atleast 1 Alphabet and 1 Number" ForeColor="Red" />
Valid Password Examples: pass1234 OR PaSs1234 OR PASS1234
Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character
<asp:TextBox ID="txtPolicy2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex2" runat="server" ControlToValidate="txtPolicy2"
ValidationExpression="^(?=.[A-Za-z])(?=.\d)(?=.[$@$!%#?&])[A-Za-z\d$@$!%*#?&]{8,}$"
ErrorMessage="Minimum 8 characters atleast 1 Alphabet, 1 Number and 1 Special Character" ForeColor="Red" />
Valid Password Examples: pass@123 OR PaSS#123 OR PASS@123
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number
<asp:TextBox ID="txtPolicy3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex3" runat="server" ControlToValidate="txtPolicy3"
ValidationExpression="^(?=.[a-z])(?=.[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet and 1 Number" ForeColor="Red" />
Valid Password Examples: PaSs1234 OR pASS1234
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
<asp:TextBox ID="txtPolicy4" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex4" runat="server" ControlToValidate="txtPolicy4"
ValidationExpression="^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[$@$!%?&])[A-Za-z\d$@$!%?&]{8,}"
ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character" ForeColor="Red" />
Valid Password Examples: PaSs@123 OR pAss@123
Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character
<asp:TextBox ID="txtPolicy5" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="Regex5" runat="server" ControlToValidate="txtPolicy5"
ValidationExpression="^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[$@$!%?&])[A-Za-z\d$@$!%?&]{8,10}"
ErrorMessage="Password must contain: Minimum 8 and Maximum 10 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character"
ForeColor="Red" />
Valid Password Examples: PaSs@123