Ahe*_*eho 32 html javascript asp.net textbox uppercase
我正在编写一个ASP.NET应用程序.我在webform上有一个文本框,我想强制任何用户类型为大写.我想在前端做这件事.您还应该注意,此文本框上有一个验证控件,因此我想确保该解决方案不会干扰ASP.NET验证.
澄清: CSS文本转换似乎使用户输入以大写形式显示.然而,在引擎盖下,由于验证控制失败,它仍然是小写的.您看,我的验证控件检查是否输入了有效的状态代码,但是我使用的正则表达式仅适用于大写字符.
Rya*_*ott 52
为什么不使用CSS和后端的组合?使用:
style='text-transform:uppercase'
Run Code Online (Sandbox Code Playgroud)
在TextBox上,在你的代码隐藏中使用:
Textbox.Value.ToUpper();
Run Code Online (Sandbox Code Playgroud)
您还可以在验证程序上轻松更改正则表达式以使用小写和大写字母.这可能是比强迫它们更容易的解决方案.
bil*_*llb 22
在文本框中使用CSS样式.你的CSS应该是这样的:
.uppercase
{
text-transform: uppercase;
}
<asp:TextBox ID="TextBox1" runat="server" Text="" CssClass="uppercase"></asp:TextBox>;
Run Code Online (Sandbox Code Playgroud)
好的,经过测试,这是一个更好,更清洁的解决方案.
$('#FirstName').bind('keyup', function () {
// Get the current value of the contents within the text box
var val = $('#FirstName').val().toUpperCase();
// Reset the current value to the Upper Case Value
$('#FirstName').val(val);
});
Run Code Online (Sandbox Code Playgroud)
小智 8
**I would do like:
<asp:TextBox ID="txtName" onkeyup="this.value=this.value.toUpperCase()" runat="server"></asp:TextBox>**
Run Code Online (Sandbox Code Playgroud)
您可以拦截按键事件,取消小写事件,并将其大写版本附加到输入:
window.onload = function () {
var input = document.getElementById("test");
input.onkeypress = function () {
// So that things work both on Firefox and Internet Explorer.
var evt = arguments[0] || event;
var char = String.fromCharCode(evt.which || evt.keyCode);
// Is it a lowercase character?
if (/[a-z]/.test(char)) {
// Append its uppercase version
input.value += char.toUpperCase();
// Cancel the original event
evt.cancelBubble = true;
return false;
}
}
};
Run Code Online (Sandbox Code Playgroud)
这适用于Firefox和Internet Explorer.你可以在这里看到它.
| 归档时间: |
|
| 查看次数: |
101162 次 |
| 最近记录: |